简体   繁体   English

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

[英]Linq to Entities: adding a where condition to a child relationship

For example I have a list of customers which each have a list of orders. 例如,我有一个客户列表,每个客户都有一个订单列表。 Now, I want to get a list of all customers with unpaid orders (let's say this is status 2). 现在,我想获得所有未付订单的客户的清单(假设这是状态2)。 Together with that list of customers, I want to have the list of unpaid orders also. 与该客户列表一起,我还希望获得未付款订单的列表。

For example I have this: 例如我有这个:

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

Where or how do I add the condition to look for Orders with status == 2 and how to include these orders with the list of customers? 我在哪里或如何添加条件以查找状态== 2的订单,以及如何将这些订单包含在客户列表中?

otherwise 除此以外

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

or 要么

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

Try this: 尝试这个:

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

Or why not simply do this: 或者为什么不简单地这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM