[英]EF Core Linq expression could not be translated
例如,當我針對可為空的 Guid 屬性進行過濾時,我遇到了 EF Core 查詢問題
public class Order
{
public Guid? MachineId {get;set;}
}
我正在嘗試根據MachineId
列表過濾訂單
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.
我不確定如何以另一種方式表達這個查詢 - 任何人都可以幫忙嗎?
將 Guid 列表轉換為可為 null 的 Guid 列表解決了該問題(感謝mjwills )。
var machineIds = machines
.Select(x => new Guid?(x.Id));
EF Core 查詢變為
var orders = _context.Orders.Where(x => machineIds.Contains(x.MachineId));
我建議刪除.GetValueOrDefault()
的使用, .GetValueOrDefault()
成為可空 guid(而不是 guid)的列表。
這可能看起來像:
var machineIds = new List<Guid?>();
var orders = _context.Orders.Where(x => machineIds.Contains(x.MachineId));
_context.Orders.Where(order => MachineIds.Where(Id => Id == order.MachineId).Any())
這是未經測試的。 可能需要進行修改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.