c# - Linq 到实体 : adding a where condition to a child relationship

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

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

例如我有这个:

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

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

最佳答案

否则

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

关于c# - Linq 到实体 : adding a where condition to a child relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2228227/

相关文章:

java - c# RSA 使用私钥加密

c# - 使用表达式 <Func<>> 和 <T> 的 Where 子句

c# - 在 linux c# 上运行信号量

c# - 将值存储在字典中或从连接的键中检索

C# 使用 LINQ 过滤对象列表中具有最大值的每个对象

c# - 基于 app.config 值的不同 app.config appSettings

c# - 使用 IoC 动态选择接口(interface)实现

.net - ASP.Net MVC 3 - HTML 扩展

c# - 有没有一种有效的方法来用两个变量做一个选择语句?

java - 统一安卓 : Didn't find class "com.unity3d.player.ReflectionHelper"