LINQ Where(predicate)vs. FirstOrDefault(predicate) [英] LINQ Where(predicate) vs. FirstOrDefault(predicate)

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

问题描述

之间有明显的性能差异:

Are there any appreciable performance differences between:

something.Where(predicate).FirstOrDefault();

something.FirstOrDefault(predicate);

,但我想知道是否有一个明确的胜利者,当谈到性能。

I tend to use both, but am wondering if there's a clear winner when it comes to performance.

推荐答案

这取决于是否其中针对 IQueryable IEnumerable
IQueryable 的情况下,区别是基于提供程序的实现,但更有可能没有差异,并且会产生相同的查询。

It depends on whether this Where is against an IQueryable or IEnumerable. In case of IQueryable the difference is based on implementation of the provider but it is more likely there will be no difference and would yield same query.

在IEnumerable的情况下,它应该是可以忽略的。

In case of IEnumerable it should be negligible.

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

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