Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find() vs. Where().FirstOrDefault()

I often see people using Where.FirstOrDefault() to do a search and grab the first element. Why not just use Find()? Is there an advantage to the other? I couldn't tell a difference.

namespace LinqFindVsWhere {     class Program     {         static void Main(string[] args)         {             List<string> list = new List<string>();             list.AddRange(new string[]             {                 "item1",                 "item2",                 "item3",                 "item4"             });              string item2 = list.Find(x => x == "item2");             Console.WriteLine(item2 == null ? "not found" : "found");             string item3 = list.Where(x => x == "item3").FirstOrDefault();             Console.WriteLine(item3 == null ? "not found" : "found");             Console.ReadKey();         }     } } 
like image 359
KingOfHypocrites Avatar asked Feb 17 '12 20:02

KingOfHypocrites


People also ask

What is the difference between FirstOrDefault and find?

The key thing to notice here is that FirstOrDefault is called on Enumerable whereas Find is called as a method on the source list. So it's iterating over an array of items which makes sense, since a list is a wrapper on an array.

What is faster than FirstOrDefault?

Code walkthrough I hope now you know that retrieving the data by a DictionaryKey is very very faster than FirstOrDefault() LINQ function. Here we see a very huge difference in execution time to retrieve the data in both ways.

Which is faster SingleOrDefault or FirstOrDefault?

c# - FirstOrDefault is signicantly faster than SingleOrDefault while viewing ANTS profiler - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

What is difference between FirstOrDefault and SingleOrDefault in Linq?

FirstOrDefault() - Same as First(), but not thrown any exception or return null when there is no result. Single() asserts that one and only one element exists in the sequence. First() simply gives you the first one.


2 Answers

Where is the Find method on IEnumerable<T>? (Rhetorical question.)

The Where and FirstOrDefault methods are applicable against multiple kinds of sequences, including List<T>, T[], Collection<T>, etc. Any sequence that implements IEnumerable<T> can use these methods. Find is available only for the List<T>. Methods that are generally more applicable, are then more reusable and have a greater impact.

I guess my next question would be why did they add the find at all. That is a good tip. The only thing I can think of is that the FirstOrDefault could return a different default value other than null. Otherwise it just seems like a pointless addition

Find on List<T> predates the other methods. List<T> was added with generics in .NET 2.0, and Find was part of the API for that class. Where and FirstOrDefault were added as extension methods for IEnumerable<T> with Linq, which is a later .NET version. I cannot say with certainty that if Linq existed with the 2.0 release that Find would never have been added, but that is arguably the case for many other features that came in earlier .NET versions that were made obsolete or redundant by later versions.

like image 169
Anthony Pegram Avatar answered Sep 18 '22 13:09

Anthony Pegram


I just found out today, doing some tests on a list of 80K objects and found that Find() can be up to 1000% faster than using a Where with FirstOrDefault(). I didn't know that until testing a timer before and after each all. Sometimes it was the same time, otherwise it was faster.

like image 21
digiben Avatar answered Sep 22 '22 13:09

digiben