实体框架 - 包括上导航属性选择条件 [英] Entity Framework - Selective Condition on Included Navigation Property

查看:140
本文介绍了实体框架 - 包括上导航属性选择条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这些简化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;}
}

业务逻辑:

一个订单可以有多个POS,一个是订单(供应商在项目级别确定)。

An order can have multiple POs, one for each distinct vendor on the order (vendors are determined at the Item level).

我如何能够有选择性包括孩子在每个不同的供应商实体?

在查询采购订单,我想自动包括订单和项目子entites的。

When querying for POs, I want to automatically include child entites for Order and Item.

我做到这一点,使用include()...

I accomplish this, using Include()...

Context.PurchaseOrders.Include("Orders.Items");

这确实是工作,拉回到相关的实体,但是, 我只想包括项目实体的厂商ID的PurchaseOrder的实体的厂商ID

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.

通过传统的SQL比赛,我只是包括在JOIN条件,但EF建立的内部。

With traditional SQL, I'd just include that in the JOIN condition, but EF builds those internally.

什么LINQ魔法我可以使用告诉EF申请的情况下,无需手动创建实体之间的联接?

What LINQ magic can I use tell EF to apply the condition, without manually creating the JOINs between the entities?

推荐答案

您不能选择性拉回符合特定条件下,某些子实体。你能做的最好的是手动筛选出相关命令自己。

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.

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 IEnumerable<Order> MatchingOrders {
         get {
            return this.Orders.Where(o => o.VendorId == this.VendorId);
         }
     }
}

这篇关于实体框架 - 包括上导航属性选择条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆