Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Expression - Using .Any in s Select Could Not Be Translated

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(). 
like image 768
sadullah zolfqar Avatar asked Sep 15 '25 18:09

sadullah zolfqar


1 Answers

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)
like image 82
Ivan Stoev Avatar answered Sep 17 '25 08:09

Ivan Stoev