A query:
List<Guid> productCodes = GetProductCodes();
var products = await _context
.Products
.Include(x => x.Supplier).Include(x => x.Warehouse).AsSplitQuery().OrderBy(x => x.Id)
.Where(x => productCodes.Contains(x.ProductCode))
.GroupBy(x => x.ProductCode).Select(x => x.First()) // workaround for `DistinctBy`
.Select(x => new { SupplierId = x.Supplier.Id, WarehouseId = x.Warehouse.Id, x.ProductCode, x.Price })
.AsNoTracking()
.ToListAsync();
Result:
InvalidOperationException: The LINQ expression ‘ProjectionBindingExpression: 0’ could not
be translated. Either rewrite the query in a form that can be translated, or switch to
client evaluation explicitly by inserting a call to ‘AsEnumerable’, ‘AsAsyncEnumerable’,
‘ToList’, or ‘ToListAsync’.
I use the .GroupBy(x => x.Foo).Select(x => x.First())
workaround instead of DistinctBy(x => x.Foo)
which is currently unsupported.
I assume from the error message that the projection in .Select()
is the problem; when I remove that the query works.
I could perform the projection afterwards, but then I’d be fetching large quantities of data for nothing.
Is there a way to fix/workaround this?