在使用 Entity Framework 5 Code First 检索一组数据时,我偶然发现了一个小问题。这是我的代码:
var data = context.Customers
.Include("Orders")
.Include("Orders.Items")
.Include("Orders.Items.Article").ToArray();
foreach (var customer in data)
{
customer.Orders = customer.Orders.OrderByDescending(o => o.OrderDate).ToArray();
}
这有效(哇!)。但是,我正试图摆脱它foreach
,我希望数据库引擎继续负责处理数据。我该怎么做呢?
我试过这种类型的加入:
var data = from customer
in context.Customers
select new {
customer.Id,
customer.Name,
// customer.Orders below doesn't have an Include() to include order items
Orders = customer.Orders.OrderByDescending(b => b.OrderDate)
};
// Translate anonymous object to actual models
var customers = new List<CustomerModel>();
foreach (var customer in data)
{
customers.Add(new CustomerModel()
{
Id = customer.Id,
Name = customer.Name,
Orders = customer.Orders.ToArray()
});
}
但是,正如评论所说,customer.Orders
没有一种Include()
方法来包含属于这些项目的项目和文章。然后我尝试从对象context
而不是customer
对象进入:
Orders = context.Orders.Include("Items").Where(o => o.Id == customer.Id)
但这会导致Include()
无法以这种方式使用的运行时异常(在嵌套查询中?):
Method 'System.Data.Entity.Infrastructure.DbQuery`1[MyTests.Models.OrderModel] Include(System.String)' declared on type 'System.Data.Entity.Infrastructure.DbQuery`1[MyTests.Models.OrderModel]' cannot be called with instance of type 'System.Data.Objects.ObjectQuery`1[MyTests.Models.OrdersModel]'
像这样嵌套它甚至可能吗?
如何让数据库处理数据而不是foreach
在我的第一个代码块中使用 a?
提前致谢。
J.P