Details
-
Bug
-
Resolution: Done
-
Minor - P4
-
None
-
2.4.3
-
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
);