实体框架 - 包含的导航属性的选择性条件
假设我有这些简化的 EF 生成的实体...
public class PurchaseOrder
{
public int POID {get;set;}
public int OrderID {get;set;}
public int VendorID {get;set;}
public IEnumerable<Order> Orders {get;set;}
}
public class Order
{
public int OrderID {get;set;}
public decimal Price {get;set;}
public IEnumerable<Item> Items {get;set;}
}
public class Item
{
public int OrderID {get; set;}
public string SKU {get;set;}
public int VendorID {get;set;}
public Order Order {get;set;}
}
业务逻辑:
一个订单可以有多个采购订单,订单上的每个不同供应商都有一个采购订单(供应商在项目中确定)等级)。
如何有选择地包含子实体?
查询采购订单时,我想自动包含订单和项目的子实体。
我使用 Include() 完成此操作...
Context.PurchaseOrders.Include("Orders.Items");
这完成了它的工作并拉回相关实体,但是,我只想包含 VendorID 与 PurchaseOrder 实体的 VendorID 匹配的 Item 实体< /强>。
对于传统 SQL,我只需将其包含在 JOIN 条件中,但 EF 在内部构建这些条件。
我可以使用什么 LINQ 魔法告诉 EF 应用条件,而无需在实体之间手动创建 JOIN?
Assume I have these simplified EF generated entities...
public class PurchaseOrder
{
public int POID {get;set;}
public int OrderID {get;set;}
public int VendorID {get;set;}
public IEnumerable<Order> Orders {get;set;}
}
public class Order
{
public int OrderID {get;set;}
public decimal Price {get;set;}
public IEnumerable<Item> Items {get;set;}
}
public class Item
{
public int OrderID {get; set;}
public string SKU {get;set;}
public int VendorID {get;set;}
public Order Order {get;set;}
}
Business Logic:
An order can have multiple POs, one for each distinct vendor on the order (vendors are determined at the Item level).
How Can I selectively Include Child Entities?
When querying for POs, I want to automatically include child entites for Order and Item.
I accomplish this, using Include()...
Context.PurchaseOrders.Include("Orders.Items");
This does it's job and pulls back related entities, but, I only want to include Item entities whose VendorID matches the VendorID of the PurchaseOrder entity.
With traditional SQL, I'd just include that in the JOIN condition, but EF builds those internally.
What LINQ magic can I use tell EF to apply the condition, without manually creating the JOINs between the entities?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法有选择地撤回符合特定条件的某些子实体。您能做的最好的事情就是自己手动过滤掉相关订单。
You can't selectively pull back certain child entities that match a certain condition. The best you can do is manually filter out the relevant orders yourself.
你不能。 EF 不允许存在急切加载的条件。您必须使用多个查询,例如:
或者您可以在单个查询中使用投影:
You can't. EF doesn't allow conditions for eager loading. You must either use multiple queries like:
Or you can use projection in single query:
您可以在此处使用 IQueryable-Extensions:
https://github.com/thiscode/DynamicSelectExtensions
扩展构建动态匿名类型。这将用于投影,如 @Ladislav-Mrnka 所描述。
然后你可以这样做:
You could use the IQueryable-Extensions here:
https://github.com/thiscode/DynamicSelectExtensions
The Extension builds dynamically an anonymous type. This will be used for projection as described by @Ladislav-Mrnka.
Then you can do this: