代码之家  ›  专栏  ›  技术社区  ›  Gerrie Schenck

Linq到实体:向子关系添加where条件

  •  5
  • Gerrie Schenck  · 技术社区  · 16 年前

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

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

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

    2 回复  |  直到 16 年前
        1
  •  3
  •   user57508 user57508    16 年前

    否则

    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
    
        2
  •  2
  •   Steven    16 年前

    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