【发布时间】:2022-09-13 23:41:40
【问题描述】:
我能够使用 EF Core 运行各种动态组合的查询除非包含子查询.
因此,这不是 EF Core dynamic lambda subquery not working 或我能找到的任何其他内容的重复。
可以在 dotnetfiddle.net/4opEqr 上找到演示该问题的可运行重现,它使用表示以下查询的动态组合表达式:
efContext.Products.Where(p => p.Id == efContext.OrderItem.Max(i => i.ProductId)).ToList();
我得到的例外是
System.InvalidOperationException : The LINQ expression 'InternalDbSet<OrderItem> { }
.Max(i => i.ProductId)' 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'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我观察到相同的行为Microsoft.EntityFrameworkCore.SqlServer与Microsoft.EntityFrameworkCore.InMemory(版本 6.0.8 和 7.0.0-preview.7.22376.2)。
【问题讨论】:
-
你想建立什么?
DistinctBy? -
这只是演示似乎适用于任何子查询的问题的简短示例。
-
背景信息:这是我提出问题的动机github.com/6bee/Remote.Linq/issues/112
-
看起来您正在尝试解决 Remote.Linq 序列化的问题?
-
@IvanStoev,很高兴知道您可以通过网络执行 LINQ 查询。我很难想象实际的用法,但有时它是需要的。 2 年前,当 OP 试图通过 TCP/IP 执行 EF 生成的 SQL 并返回结果时,回答了类似的问题。
标签: entity-framework-core subquery