Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Where(predicate) vs. FirstOrDefault(predicate)

Tags:

linq

c#-4.0

Are there any appreciable performance differences between:

something.Where(predicate).FirstOrDefault();

and

something.FirstOrDefault(predicate);

?

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

like image 771
Major Productions Avatar asked Dec 26 '22 20:12

Major Productions


1 Answers

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.

In case of IEnumerable it should be negligible.

like image 113
Muhammad Hasan Khan Avatar answered Feb 15 '23 09:02

Muhammad Hasan Khan