如何在Linq中使用OrderBy [英] How to use OrderBy in Linq

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

问题描述

我正在使用OrderBy,并且我已经发现必须将OrderBy作为最后一种方法,否则它将无法正常工作.不同的运算符不授予其将保持值的原始顺序的权限,或者如果我使用Include,则它不能对子级集合进行排序.

I am using OrderBy, and I have figured out that I have to use OrderBy as a last method, or it will not work. Distinct operator does not grant that it will maintain the original order of values, or if I use Include, it cannot sort the children collection.

我有什么理由不应该让Orderby永远持续下去并且不用担心订单是否被保留吗?

Is there any reason why I shouldn't do Orderby always last and don't worry if order is preserved?

通常,是否有任何原因(例如对性能的影响)为何我不应该最后使用OrderBy.如果我使用EnityFramework来查询数据库或只是查询某些集合,就不会发生问题.

In general, is there any reason, like performance impact, why I should not use OrderBy last. Doesnt metter if I use EnityFramework to query a database or just querying some collection.

dbContext.EntityFramework.Distinct().OrderBy(o=> o.Something); // this will give me ordered result 

dbContext.EntityFramework.OrderBy(o=> o.Something).Distinct().; // this will not, because Distinct doesnt preserve order.

让我们说我只想选择一个属性.

Lets say that I want to Select only one property.

dbContext.EntityFramework.Select(o=> o.Selected).OrderBy(o=> o.Something);

如果在选择一个属性后订购集合,订购会更快吗?因此,在这种情况下,我应该使用最后订购".我只是问是否有任何情况应作为最后一条命令进行订购?

Will order be faster if I order collection after one property selection? So in that case I should use Order last. And I am just asking is there any situation where ordering shoudnt be done as last command?

推荐答案

我有什么理由不应该让OrderBy永远持续吗?

Is there any reason why I shouldn't do OrderBy always last

可能有理由不使用 OrderBy 作为最后一条语句.例如,结果中可能没有sort属性:

There may be reasons to use OrderBy not as the last statement. For example, the sort property may not be in the result:

var result = context.Entities
                    .OrderBy(e => e.Date)
                    .Select(e => e.Name);

或者您希望将排序后的集合作为结果的一部分:

Or you want a sorted collection as part of the result:

var result = context.Customers
                    .Select(c => new
                    {
                        Customer = c,
                        Orders = c.Orders.OrderBy(o => o.Date)
                        Address = c.Address
                    });

如果在选择一个属性后订购集合,订购会更快吗?

Will order be faster if I order collection after one property selection?

您的示例表明您正在使用LINQ to Entities,因此该语句将转换为SQL.您会注意到...

Your examples show that you're working with LINQ to Entities, so the statements will be translated into SQL. You will notice that...

context.Entities
       .OrderBy(e => e.Name)
       .Select(e => e.Name)

...和...

context.Entities
       .Select(e => e.Name)
       .OrderBy(s => s)

...将产生完全相同的SQL.因此,两个 OrderBy 位置之间没有本质区别.

... will produce exactly the same SQL. So there is no essential difference between both OrderBy positions.

我使用实体框架查询数据库还是只是查询某些集合都没关系.

Doesn't matter if I use Entity Framework to query a database or just querying some collection.

好吧,确实很重要.例如,如果您这样做...

Well, that does matter. For example, if you do...

context.Entities
       .OrderBy(e => e.Date)
       .Select(e => e.Name)
       .Distinct()

...您会注意到 OrderBy 被EF完全忽略,名称的顺序是不可预测的.

... you'll notice that the OrderBy is completely ignored by EF and the order of names is unpredictable.

但是,如果您愿意...

However, if you do ...

context.Entities
       .AsEnumerable() // Continue as LINQ to objects
       .OrderBy(e => e.Date)
       .Select(e => e.Name)
       .Distinct()

...,您将看到排序结果保留在不同的结果中.LINQ to Object显然与LINQ to Entities具有不同的策略.语句末尾的 OrderBy 会使两个结果相等.

... you'll see that the sort order is preserved in the distinct result. LINQ to objects clearly has a different strategy than LINQ to Entities. OrderBy at the end of the statement would have made both results equal.

总而言之,我想说,根据经验,尝试在LINQ查询中尽可能晚地订购.这将产生最可预测的结果.

To sum it up, I'd say that as a rule of the thumb, try to order as late as possible in a LINQ query. This will produce the most predictable results.

这篇关于如何在Linq中使用OrderBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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