Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2008

Member in projection cannot be translated.

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Minor - P4 Minor - P4
    • None
    • 2.4.3
    • Linq
    • None
    • Windows 10, VS 2017, netcoreapp1.1

    Description

      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) }

      );

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            kvanover Koen Vanoverloop
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: