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 ot
Find()
is the IEnumerable equivalent of a FirstOrDefault()
. You should not chain both .Where() with .FirstOrDefault()
because the .Where()
goes through the whole array and then will iterate through that list to find the first item. You save an incredible amount of time by putting your search predicate in the FirstOrDefault()
method.
Also, I encourage you to read the linked question to this thread to know more about the better performances on using the .Find()
in specific scenarios Performance of Find() vs. FirstOrDefault()