Asp.net Core 3.1 LINQ Expression group by and select from a table and I use any into select but there is an error.
But it works fine in asp.net standard.
Code:
List<GetObj> liste = dbContext.testTable
.Where(x => x.isActive == true).OrderByDescending(x => x.Id)
.GroupBy(x => new { x.field1, x.field2 })
.Select(x => new GetObj
{
field1 = x.Key.field1,
field2 = x.Key.field2,
totalQuantity = x.Sum(y => y.ldNet),
isMaped = x.Any(y => y.isLastMove == true)
}).ToList();
And the resulted error is:
.Any(y => y.isLastMove == True)' 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
Currently (EF Core 3.x) only projection of key / scalar aggregates is supported for GroupBy queries, and Any
does not fall into that category.
A bit unusual and not so readable, but since Any
returns false
if all element conditions are false
, and also true > false
, Any
can be replaced with the supported Max
aggregate:
isMaped = x.Max(y => y.isLastMove)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With