【发布时间】:2020-08-26 14:29:17
【问题描述】:
例如,当我针对可为空的 Guid 属性进行过滤时,我遇到了 EF Core 查询问题
public class Order
{
public Guid? MachineId {get;set;}
}
我正在尝试根据MachineIds 列表过滤订单
var machineIds // a list of Guids
var orders = _context.Orders.Where(x => machineIds.Contains(x.MachineId.GetValueOrDefault()));
我得到的错误是
System.InvalidOperationException : The LINQ expression 'DbSet<Orders>
.Where(x => __machineIds_0
.Contains(m.MachineId.GetValueOrDefault()))' 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(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我不确定如何用另一种方式表达这个查询 - 谁能帮忙?
【问题讨论】:
-
如果您删除
.GetValueOrDefault()并将machineIds设为可为空的 guid(而不是 guid)列表会怎样? -
有什么问题?
-
我建议您在此处打开一个包含最小重现案例的错误报告。他们应该能够在 efcore github 上为您提供帮助——或者至少将其视为一个错误。这看起来真的应该工作 - 但显然它没有,这是 LINQ 翻译中的一个漏洞(其中一个)。打开错误报告有助于他们慢慢解决问题。
-
@mjwills 成功了 - 谢谢!!
标签: c# linq entity-framework-core