【问题标题】:EF Linq - how to select children in same query?EF Linq - 如何在同一个查询中选择孩子?
【发布时间】:2014-09-29 14:53:56
【问题描述】:

我有 Oracle db,上面有 EF 5。

假设我有表 Company 和 Orders。我有公司有字段的 EF 对应实体

List<Orders> Orders

我不想创建关联

我有一个 IQuerable 查询,我需要使用

填写每个公司的订单

order.CompanyId == company.Id

我无法在这台自动取款机周围转转。任何帮助将不胜感激。

编辑:并非每家公司都有订单。订单列表可能为空。

【问题讨论】:

    标签: c# linq entity-framework linq-to-entities


    【解决方案1】:

    我会考虑使用.Join() 方法来包含Orders 表。我是凭记忆写的,所以请原谅语法错误。

    //I only use this bc I don't know the full schema.
    class DTOCompany {
      public int ID {get;set;}
      public List<Orders> Orders {get;set;}
    }
    
    public List<Companies> GetCompaniesOrders() {
    
      using (var db = new Entities()) {
        return db.Companies
          .AsEnumerable() //may not be needed.
          .Join(db.Orders,
            c => CompanyId,
            o => o.CompanyId,
            (c,o) => new { Company = c, Orders = o})
          )
          .Select(co => new DTOCompany() {
            ID = co.Companies.CompanyId,
            Orders = co.Orders.ToList()
          })
          .ToList();
      }
    }
    

    【讨论】:

    • 对不起,我没有说得更清楚,但不是每个公司都有订单。订单列表可能为空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多