Linq to Entities:为子关系添加where条件

时间:2010-02-09 10:13:25

标签: c# .net linq linq-to-entities

例如,我有一个客户列表,每个客户都有一个订单列表。现在,我希望获得所有未付订单的客户列表(假设这是状态2)。与该客户列表一起,我还希望获得未付订单列表。

例如我有这个:

from c in mycontext.Customers.Include("Orders")
select c

在何处或如何添加条件以查找状态为== 2的订单以及如何将这些订单包含在客户列表中?

2 个答案:

答案 0 :(得分:3)

,否则

from c in mycontext.Customers.Include("Orders")
where c.Orders.Any(order => order.status == 2)
select c

from c in mycontext.Customers.Include("Orders")
let newObject = {
    Customer = c,
    NotPaidOrders = c.Orders.Where(order => order.status == 2).ToList()
}
where newObject.NotPaidOrders.Any()
select newObject

答案 1 :(得分:2)

试试这个:

from c in mycontext.Customers.Include("Orders")
from order in c.Orders
where order.status == 2
select order

或者为什么不简单地这样做:

from order in mycontext.Orders
where order.status == 2
select order