如何在ADO.NET实体框架中用热心加载来排序相关实体 [英] How to sort related entities with eager loading in ADO.NET Entity Framework

查看:101
本文介绍了如何在ADO.NET实体框架中用热心加载来排序相关实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,



考虑到Northwind示例表客户,订单和OrderDetails我想要加载与上述表相对应的相关实体,但我需要在获取实体之前,请先命名数据库上的子实体。



基本案例:

  var someQueryable =来自northwindContext中的客户。 Customers.Include(Orders.OrderDetails)
选择客户;

但我还需要在数据库端排序Orders和OrderDetails(在将这些实体提取到内存之前)相对于这些表上的一些随机列。是否可能没有一些投影,就像在T-SQL中?解决方案是否使用e-SQL或LINQ to Entities无关紧要。我搜索了网络,但我对我发现的答案不满意,因为他们主要涉及将数据投影到一些匿名类型,然后重新查询匿名类型,以按照您喜欢的顺序获取子实体。同样使用CreateSourceQuery()似乎并不是我的选择,因为我需要在数据库端获取数据,而不是加载,而是通过排序子实体。那就是我要在执行任何查询之前做ORDER BY,然后按照我想要的顺序取得实体。提前感谢任何指导。作为一个个人笔记,请原谅直接的语言,因为我很生气,微软发布了这样一个不成熟的形式的EF,即使是Linq到SQL(他们似乎正在慢慢消失)。我希望这个EF的东西会变得更好,在.NET FX 4.0的发布版本中没有明显的错误。

解决方案

其实我提示完全解决了这个问题。



相关实体的排序不是支持,但使用投影方式Craig显示 AND 依赖在一些叫做关系修正的东西上,你可以得到类似的工作:



如果你这样做:

  var projection = from c in ctx.Customers 
select new {
Customer = c,
Orders = c.Orders.OrderByDescending(
o =   o.OrderDate

};

foreach(var anon in projection)
{
anon.Orders //被排序(因为投影)
anon.Customer.Orders //被排序太!因为关系fixup
}

这意味着如果你这样做:

  var customers = projection.AsEnumerable()。选择(x => x.Customer); 

您将有客户排序订单!



希望这有帮助



Alex


Greetings,

Considering the Northwind sample tables Customers, Orders, and OrderDetails I would like to eager load the related entities corresponding to the tables mentioned above and yet I need ot order the child entities on the database before fetching entities.

Basic case:

var someQueryable = from customer in northwindContext.Customers.Include("Orders.OrderDetails") 
select customer;

but I also need to sort Orders and OrderDetails on the database side (before fetching those entities into memory) with respect to some random column on those tables. Is it possible without some projection, like it is in T-SQL? It doesn't matter whether the solution uses e-SQL or LINQ to Entities. I searched the web but I wasn't satisfied with the answers I found since they mainly involve projecting data to some anonymous type and then re-query that anonymous type to get the child entities in the order you like. Also using CreateSourceQuery() doesn't seem to be an option for me since I need to get the data as it is on the database side, with eager loading but just by ordering child entities. That is I want to do the "ORDER BY" before executing any query and then fetch the entities in the order I'd like. Thanks in advance for any guidance. As a personal note, please excuse the direct language since I am kinda pissed at Microsoft for releasing the EF in such an immature shape even compared to Linq to SQL (which they seem to be getting away slowly). I hope this EF thingie will get much better and without significant bugs in the release version of .NET FX 4.0.

解决方案

Actually I have Tip that addresses exactly this issue.

Sorting of related entities is not 'supported', but using the projection approach Craig shows AND relying on something called 'Relationship Fixup' you can get something very similar working:

If you do this:

var projection = from c in ctx.Customers
                 select new {
                       Customer = c, 
                       Orders = c.Orders.OrderByDescending( 
                                 o => o.OrderDate 
                             )
                 };

foreach(var anon in projection )
{
   anon.Orders //is sorted (because of the projection)
   anon.Customer.Orders // is sorted too! because of relationship fixup
}

Which means if you do this:

var customers = projection.AsEnumerable().Select(x => x.Customer);

you will have customers that have sorted orders!

See the tip for more info.

Hope this helps

Alex

这篇关于如何在ADO.NET实体框架中用热心加载来排序相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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