[CSHARP-2008] Member in projection cannot be translated. Created: 26/Jun/17  Updated: 26/Nov/18  Resolved: 26/Nov/18

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.4.3
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Koen Vanoverloop Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

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

);



 Comments   
Comment by Robert Stam [ 23/Oct/18 ]

I am trying to reproduce this but there is not enough information to compile without errors and test it.

There are a number of types that I don't have definitions for (although I can guess at them).

And the following line does not compile

var query = carriers.AsQueryable().GroupJoin(carrierContracts, x => x.Id, x => x.CarrierId, (carrier, contracts) => new { carrier, contracts });

because the carrierContracts argument to GroupJoin does not implement IEnumerable<T>.

Can you provide a sample that can actually be compiled and run?

Comment by Koen Vanoverloop [ 07/Jul/17 ]

We do have a workaround for the moment, but it is not optimal.

So, if you can take a look in why the local variables don't translate well, It should be be nice.

Thanks,

Koen

Comment by Craig Wilson [ 05/Jul/17 ]

Hi Koen,

On the surface, this appears odd to me. I don't see anything weird about your code, so this is probably a bug inside our code related to expanding captured variables. Do have a workaround at this point?

Craig

Generated at Wed Feb 07 21:41:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.