用Linq + Include排序 [英] Order by with Linq + Include

查看:78
本文介绍了用Linq + Include排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与两个实体存在一对多关系:

I have a One-Many relation with two Entities :

Order:
int OrderId 
string OrderNumber
...

OrderItem:
int ItemId
int sequence
...

Product:
int ProductId
string ProductName

ProductType:
int ProductTypeid
string Title

一个 Order 具有多个 OrderItems ,并且每个 OrderItem 具有一个 Product 和每个 Product 具有一个 ProductType .

One Order Has multiple OrderItems, and each OrderItem has One Product and each Product has one ProductType.

我想编写一个Linq,以返回所有带有其 items Product ProductType 的订单,并且按顺序字段对项目进行排序.如何编写查询,例如以下查询?

I want to write a Linq that return all orders with their items,Product,ProductType and items be sorted by sequence field. How can I write a query such as following query?

通过Linq进行订购的帮助下,我编写了以下查询:

with help from Order by with Linq i wrote this query:

var myOrder= db.Orders.Include("OrderItems")
         .Where(x => x.OrderId == 3)
         .Include("OrderItems.Product") 
         .Include("OrderItems.Product.ProductType") 
         .Select(o => new {
             order = o,
             orderItems = o.OrderItems.OrderBy(i => i.sequence)
         }).FirstOrDefault();

,但是当它返回结果时,它不包含产品和产品类型数据.我的错误在哪里?

but when it return the result, it doesn't contain Product and ProductType data. where is my mistake?

推荐答案

您需要首先将所有调用移至 Include().这应该起作用:

You need to put all calls to Include() first. This should work:

var myOrder= db.Orders.Include("OrderItems")
     .Include("OrderItems.Product") 
     .Include("OrderItems.Product.ProductType") 
     .Where(x => x.OrderId == 3)
     .Select(o => new {
         order = o,
         orderItems = o.OrderItems.OrderBy(i => i.sequence)
     }).FirstOrDefault();

此外,当您具有 .Include("OrderItems.Product.ProductType")时,就不需要 .Include("OrderItems").Include("OrderItems.Product"),因为它将在包含产品类型的方式中包含OrderItems及其产品.它必须这样做,否则您将无法在代码中导航到它们-它将它们附加到什么上?

Also, when you have .Include("OrderItems.Product.ProductType"), you do not need .Include("OrderItems") and .Include("OrderItems.Product"), because it will include OrderItems and their products on the way to including product types. It has to do this or you would be unable to navigate to them in code -- what would it attach them to?

在这种情况下,似乎可以对此进行解释: http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework

In this case, it looks as though this might explain it: http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework

您可以不使用包含项而四处走动:

You can get around without using includes:

var query = db.Orders
.Where(x => x.OrderId == 3)
.Select(o => new {
order = o,
orderItems = o.OrderItems.OrderBy(i => i.sequence),
products = o.OrderItems.Select(i => new { i, i.Product, i.Product.ProductType })
});

您投影到输出选择中的所有内容都会自动加载.在某些方面,像这样的预先加载实际上是更可取的,因为您只是实现了所需的东西,而不是完整的对象图.(尽管在这种情况下,我们实现的内容与包含内容相同.)

Anything you project into your output select will be automatically eager-loaded. Eager loading like this is actually preferable in some ways, because you are only materializing what you need, rather than the full object graph. (Although in this case we're materializing the same as what the include would.)

这篇关于用Linq + Include排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆