如何使用预先加载的ADO.NET实体框架相关的实体排序 [英] How to sort related entities with eager loading in ADO.NET Entity Framework

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

问题描述

问候,

考虑到罗斯文示例表客户,订单,订单明细和我想急于负荷对应于上述的表相关的实体,但我需要获取的实体之前OT订购数据库的子实体。

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.

基本情况:

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

但我还需要就在这些表中一些随机列(取这些实体到内存中之前)进行排序在数据库方面的订单和订单明细。是否有可能没有一些投影,就像是在T-SQL?不要紧的解决方案是使用E-SQL或LINQ到实体。我在网上搜索,但我并不满足,我发现问题的答案,因为它们主要涉及投影数据,一些匿名的类型,然后再查询匿名类型来得到你喜​​欢的顺序子实体。还利用CreateSourceQuery()似乎并没有成为我的选择,因为我需要得到的数据,因为它是在数据库方面,与预先加载,但只是通过订购的子实体。这就是我要执行任何查询之前做了ORDER BY,然后取顺序我想要的实体。感谢您事先的任何指导。作为我个人而言,请原谅直接的语言,因为我有点生气,在微软发布的EF中甚至比LINQ到SQL(他们似乎越来越远慢)这样一个不成熟的形状。我希望这EF的thingie会得到更好的,没有显著的错误在.NET FX 4.0的发布版本。

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.

推荐答案

其实我有<一个href="http://blogs.msdn.com/alexj/archive/2009/02/25/tip-1-sorting-relationships-in-entity-framework.aspx">Tip这正好解决了这个问题。

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:

如果你这样做:

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.

希望这有助于

亚历

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

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