我想问为什么在客户端对这样的查询进行评估:
_context
.Items
.Include(x => x.Status)
.Include(x => x.Orders)
.ThenInclude(x => x.User)
.Include(x => x.Orders)
.ThenInclude(x => x.OrderStatus)
.Where(x => x.Orders.Any())
.Where(x => x.Order != null)
.Where(x => x.Order.User.SomeProperty.ToLower() == user.SomeProperty.ToLower());
在user.SomeProperty.ToLower()
中使用的用户仅仅是身份用户,并且不为空。
public class Item
{
public Guid Id = { get; protected set; }
public List<Order> Orders { get; set; } = new List<Order>();
public Order Order => Orders.FirstOrDefault(x => x.OrderStatus.Name = "Active");
public Status Status { get; set; }
}
public class Order
{
public Guid Id = { get; protected set; }
public User User = { get; set; }
public Status OrderStatus = { get; set; }
}
public class Status
{
public Guid Id = { get; protected set; }
public string Name = { get; set; }
}
EF核心警告说空检查是原因之一,但我不明白为什么无法翻译空检查
警告:Microsoft.EntityFrameworkCore.Query [20500] LINQ表达式'where(Property([x] .Order,“ Id”)!= null)')无法翻译,将在本地进行评估。
警告:Microsoft.EntityFrameworkCore.Query [20500] LINQ表达式'where([x] .Order.User.SomeProperty == ____ user_SomeProperty_0)'无法翻译,将在本地进行评估。
答案 0 :(得分:1)
EF无法在方法或属性中转换查询。移动
public Order Order => Orders.FirstOrDefault(x => x.OrderStatus.Name = "Active");
到实际查询
编辑。您可以使用扩展方法来重用查询
edit2:创建扩展方法
public static class FooQueryExtensions
{
public static IQueryable<FooResult> MyFooQuery(this IQueryable<Foo> source)
{
return source.SelectMany(...).Where(...); //basicly do what yuo want
}
}
使用方式
_context.Set<Foo>().MyFooQuery().Where(result => more query);