排序依据()。最后()或OrderByDescending()。第一个()的性能 [英] OrderBy().Last() or OrderByDescending().First() performance

查看:402
本文介绍了排序依据()。最后()或OrderByDescending()。第一个()的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是微优化,但我仍然不知道是否有任何区别使用

I know that this probably is micro-optimization, but still I wonder if there is any difference in using

var lastObject = myList.OrderBy(item => item.Created).Last();

var lastObject = myList.OrderByDescending(item => item.Created).First();

我要寻找答案LINQ到对象和LINQ到实体。

I am looking for answers for Linq to objects and Linq to Entities.

推荐答案

假设排序采取同样的时间的两种方式(这是一个很大的如果),那么第一种方法将有做的额外费用。去年(),可能需要一个完整的枚举。

Assuming that both ways of sorting take equal time (and that's a big 'if'), then the first method would have the extra cost of doing a .Last(), potentially requiring a full enumeration.

和这样的说法可能甚至对一个SQL面向LINQ更强。

And that argument probably holds even stronger for an SQL oriented LINQ.

这篇关于排序依据()。最后()或OrderByDescending()。第一个()的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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