c# - LINQ 表达式 - 在 s Select 中使用 .Any 无法翻译

标签 c# linq entity-framework-core

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 不属于该类别。

有点不寻常且可读性差,但是因为如果所有元素条件都为 falsetrue,则 Any 返回 false > falseAny 可以替换为受支持的 Max 聚合:

isMaped = x.Max(y => y.isLastMove)

关于c# - LINQ 表达式 - 在 s Select 中使用 .Any 无法翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61695891/

相关文章:

c# - 用于选择两个集合之间的公共(public)元素的 Linq 语句

c# - LINQ 查询基于列表的子字符串进行过滤

entity-framework - 构造函数注入(inject) IDesignTimeDbContextFactory

docker-compose - 如何从主机连接到数据库 - 在docker compose下运行的容器中的sql server

c# - 无法访问hangfire仪表板

c# - 如何在中继器内动态按钮/我总是得到相同的值

c# - 如何使用 LINQ 确定特定属性值是否存在?

c# - 添加到聚合根时未保存子实体

c# - Exchange Web Services 2010/自动完成或建议的联系人

c# - 将一个字符串与一组通配符进行比较的最快方法