C# LinqWhere(表达式).FirstorDefault() 与 .FirstOrDefault(表达式)
这两个 Linq 查询有什么区别:
var result = ResultLists().Where( c=> c.code == "abc").FirstOrDefault();
// vs.
var result = ResultLists().FirstOrDefault( c => c.code == "abc");
- 语义是否完全相同?
- 如果语义上相等,则 谓词形式的
FirstOrDefault
比Where()
加上普通的FirstOrDefault()
提供任何理论或实际性能优势>?
What is the difference between these two Linq queries:
var result = ResultLists().Where( c=> c.code == "abc").FirstOrDefault();
// vs.
var result = ResultLists().FirstOrDefault( c => c.code == "abc");
- Are the semantics exactly the same?
- Iff sematically equal, does the predicate form of
FirstOrDefault
offer any theoretical or practical performance benefit overWhere()
plus plainFirstOrDefault()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
第二个。在所有其他条件相同的情况下,第二种情况下的迭代器可以在找到匹配项后立即停止,而第一个迭代器必须找到所有匹配项,然后选择其中的第一个。
The second one. All other things being equal, the iterator in the second case can stop as soon as it finds a match, where the first one must find all that match, and then pick the first of those.
很好的讨论,以上所有答案都是正确的。
我没有运行任何性能测试,而根据我的经验,FirstOrDefault() 有时比Where().FirstOrDefault() 更快并且优化。
我最近修复了内存溢出/性能问题(“神经网络算法”),修复方法是将Where(x->...).FirstOrDefault()更改为简单的FirstOrDefault(x->..)。
我忽略了编辑将Where(x->...).FirstOrDefault()更改为简单的FirstOrDefault(x->..)的建议。
所以我相信上述问题的正确答案是
第二个选项是所有情况下的最佳方法
Nice discussion, all the above answers are correct.
I didn't run any performance test, whereas on the bases of my experience FirstOrDefault() sometimes faster and optimize as compare to Where().FirstOrDefault().
I recently fixed the memory overflow/performance issue ("neural-network algorithm") and fix was changing Where(x->...).FirstOrDefault() to simply FirstOrDefault(x->..).
I was ignoring the editor's recommendation to change Where(x->...).FirstOrDefault() to simply FirstOrDefault(x->..).
So I believe the correct answer to the above question is
The second option is the best approach in all cases
Where
实际上是延迟执行 - 这意味着表达式的计算被延迟,直到实际需要其实现的值为止。它通过避免不必要的执行来极大地提高性能。Where
看起来有点像这样,并返回一个新的 IEnumerableFirstOrDefault()
返回
并且不会抛出任何异常或返回 null没有结果Where
is actually a deferred execution - it means, the evaluation of an expression is delayed until its realized value is actually required. It greatly improves performance by avoiding unnecessary execution.Where
looks kind of like this, and returns a new IEnumerableFirstOrDefault()
returns<T>
and not throw any exception or return null when there is no result这是我的基准。
基准
所以看起来Where(n => n ==800).FirstOrDefault()比FirstOrDefault(n => n == 800)更快。请参阅基准中的图像。
Here is my benchmark.
Benchmark
So it looks like the Where(n => n ==800).FirstOrDefault() is faster than the FirstOrDefault(n => n == 800). Please see the image in benchmark.
两者都可以。
它们都延迟运行 - 如果源列表有 100 万个项目,但第十个项目匹配,那么两者都只会从源中迭代 10 个项目。
性能应该几乎相同,任何差异都完全微不足道。
Either is fine.
They both run lazily - if the source list has a million items, but the tenth item matches then both will only iterate 10 items from the source.
Performance should be almost identical and any difference would be totally insignificant.