Asp.net Core 3.1 LINQ Expression group by 和 select from a table 我使用 any into select 但有错误。
但它在 asp.net 标准中运行良好。
代码:
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();
结果错误是:
.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().
最佳答案
目前 (EF Core 3.x) 只有键/标量聚合的投影是 supported for GroupBy queries , 而 Any
不属于该类别。
有点不寻常且可读性差,但是因为如果所有元素条件都为 false
和 true,则
,Any
返回 false
> falseAny
可以替换为受支持的 Max
聚合:
isMaped = x.Max(y => y.isLastMove)
关于c# - LINQ 表达式 - 在 s Select 中使用 .Any 无法翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61695891/