Member in projection cannot be translated.

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Minor - P4
    • None
    • Affects Version/s: 2.4.3
    • Component/s: LINQ
    • None
    • Environment:
      Windows 10, VS 2017, netcoreapp1.1
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      I receive an System.NotSupportedException on the mongo driver when querying a collection:

      Abstract: "Member [xxx] of type [xxx] in the expression tree [xxx] cannot be translated."
      Concrete: Message: "Member destinationCountry of type Quantum.Carriers.Api.Controllers.CarriersController+<>c_DisplayClass2_1 in the expression tree value(Quantum.Carriers.Api.Controllers.CarriersController+<>c_DisplayClass2_1).destinationCountry cannot be translated."

      It seems there is a bug in converting the expression tree.

      private static async Task<IEnumerable<CarrierSummary>> GetCarriersWithDynamicFilters(CarrierQueryFilter filter, IMongoCollection<Carrier> carriers, IMongoCollection<CarrierContract> carrierContracts)
              {
                  var query = carriers.AsQueryable().GroupJoin(carrierContracts, x => x.Id, x => x.CarrierId, (carrier, contracts) => new { carrier, contracts });
                  Expression<Func<CarrierContract, bool>> predicate = x => true;
      
      
                  if (!string.IsNullOrWhiteSpace(filter.DestinationCountryCode))
                  {
                      query = query.Where(x => x.contracts.Any(c => c.DestinationCountryCode == filter.DestinationCountryCode));
                      predicate = c => c.DestinationCountryCode == filter.DestinationCountryCode;
                  }
      
                  if (!string.IsNullOrWhiteSpace(filter.DistributionCountryCode))
                  {
                      query = query.Where(x => x.contracts.Any(c => c.DistributionCountryCode == filter.DistributionCountryCode));
                      predicate = c => c.DistributionCountryCode == filter.DistributionCountryCode;
                  }
      
                  var res = query.Select(x => new CarrierSummary
                  {
                      Id = x.carrier.Id,
                      Name = x.carrier.Name,
                      CarrierCode = x.carrier.Code,
                      DeliveryTypes = x.contracts.AsQueryable().Where(predicate).Select(y => y.DeliveryType)
                  });
      
                  return await res.ToListAsync();
              }
      

      it works when I don't use the variable:
      predicate = c => c.DestinationCountryCode == "BE";
      or
      predicate = c => c.DistributionCountryCode == "BE";

      without expression variable, it also works:

      query.Select(c => new CarrierSummary

      { Id = c.carrier.Id, Name = c.carrier.Name, CarrierCode = c.carrier.Code, DeliveryTypes = c.contracts.Where(x => x.DistributionCountryCode == filter.DistributionCountryCode).Select(x => x.DeliveryType) }

      );

            Assignee:
            Robert Stam (Inactive)
            Reporter:
            Koen Vanoverloop
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: