【发布时间】:2017-03-08 03:32:47
【问题描述】:
我有以下型号
Deal.cs
public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public double? Price { get; set; }
public Guid UserId { get; set; }
public User User { get; set; }
用户.cs
public Guid Id { get; set; }
public string Name { get; set; }
public string ResumptionCookie { get; set; }
public List<Deal> Deals { get; set; }
我正在尝试获取当前与特定代码达成交易的所有用户。我可以获取所有用户并编写大型 LINQ 查询,但必须有更优雅的方式。
我阅读了documentation,但它并不是我想要的,而且我一直无法弄清楚。
【问题讨论】:
-
如何使用:
db.Users.Where(x=>x.Deals.Any(y=>y.Code == "your_givenCode")); -
啊,是的,这会返回一个 IEnumerable
,但就 EntityFramework 而言,这是优化的吗?我在想我需要使用 .Include或.Collections。我可能在这里过度优化,但我想避免将所有数据加载到内存中并随后对其进行过滤。 -
以上代码是延迟加载,即在查询具体化时加载,例如使用 ToList()、First()、Count() 和其他方法。但我怀疑 EF 核心是否支持延迟加载。
-
根据文档,目前 EF 核心不支持延迟加载 docs.microsoft.com/en-us/ef/efcore-and-ef6/index
-
您过于专注于加载。您不需要为此查询加载一个
Deal。 Akash 的查询完全符合您的规定。
标签: c# linq entity-framework-core