C# Linq Where(expression).FirstorDefault() vs .FirstOrDefault(expression) [英] C# Linq Where(expression).FirstorDefault() vs .FirstOrDefault(expression)

查看:42
本文介绍了C# Linq Where(expression).FirstorDefault() vs .FirstOrDefault(expression)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个 Linq 查询有什么区别:

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");

  • 语义是否完全相同?
  • 如果语义相等,FirstOrDefault 的谓词形式比 Where() 加上普通的 FirstOrDefault() 提供任何理论或实际性能优势?
    • Are the semantics exactly the same?
    • Iff sematically equal, does the predicate form of FirstOrDefault offer any theoretical or practical performance benefit over Where() plus plain FirstOrDefault()?
    • 推荐答案

      哪个都行.

      它们都懒惰地运行 - 如果源列表有 100 万个项目,但第 10 个项目匹配,那么它们都只会从源中迭代 10 个项目.

      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.

      这篇关于C# Linq Where(expression).FirstorDefault() vs .FirstOrDefault(expression)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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