[英]EF Linq - how to select children in same query?
I have Oracle db with EF 5 on top of it. 我在它上面有带有EF 5的Oracle数据库。
Lets say I have tables Company and Orders. 可以说我有公司和订单表。 I have EF corresponding entities where Company has field 我有EF对应实体,公司在其中
List<Orders> Orders
. List<Orders> Orders
。
I dont want to create an association . 我不想建立关联 。
I have a query which is IQuerable and I need to fill the Orders for each company using 我有一个查询为IQuerable, 我需要使用填写每个公司的订单
order.CompanyId == company.Id
I cant wrap my head around this atm.. Any help will be appreciated. 我不能把头缠在这个atm上。任何帮助将不胜感激。
EDIT: not every company has orders. 编辑:并非每个公司都有订单。 The orders list could be empty. 订单列表可能为空。
I would consider using the .Join() method to include the Orders table. 我会考虑使用.Join()方法包括Orders表。 I write this from memory, so please forgive syntax errors. 我是从内存中写出来的,因此请原谅语法错误。
//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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.