Can someone explain how the LINQ functions Where(..) and FindAll(..) differ? They both seem to do the same thing...
-
possible duplicate of [FindAll vs Where extension-method](http://stackoverflow.com/questions/1531702/findall-vs-where-extension-method) – Ryan Gates Dec 04 '14 at 16:43
-
[A clear answer here, where by far performs better](https://stackoverflow.com/questions/2260220/c-sharp-findall-vs-where-speed) – Eduardo A. Fernández Díaz Oct 20 '19 at 17:12
5 Answers
FindAll()
is a function on the List<T>
type, it's not a LINQ extension method like Where
. The LINQ extension methods work on any type that implements IEnumerable
, whereas FindAll
can only be used on List<T>
instances (or instances of classes that inherit from it, of course).
Additionally, they differ in actual purpose. Where
returns an instance of IEnumerable
that is executed on-demand when the object is enumerated. FindAll
returns a new List<T>
that contains the requested elements. FindAll
is more like calling Where(...).ToList()
on an instance of IEnumerable
.

- 182,639
- 35
- 285
- 343
-
33
-
4http://code.msdn.microsoft.com/LINQ-Query-Execution-ce0d3b95 explains the differences between lazy (deferred) and immediate execution. Basically, in some cases, you don't need the whole list, you may want to loop through the items until something happens, then stop. This is where lazy comes in handy, but depending on implementation, can lead to unpredictable results (all explained in the link). Hope this helps. – nurchi Jun 06 '14 at 02:59
-
Important to know, thanks for answer. As i see where uses "yield return element", while findall construct appropriate list and return. – zzfima Jul 28 '23 at 05:43
The biggest difference to me is that .FindAll is also available in .Net 2.0. I don't always have the luxury to program in .Net 3.5, so I try to remember the 'native' methods of the .Net generic collections.
It happened several times that I implemented an already available List method myself because I couldn't LINQ it.
What I find handy in this case is that, using VS2008, I can use type inference and the lambda syntax. These are compiler features, not framework features. This means I can write this and still remain within .Net 2.0:
var myOddNums = myNums.FindAll(n => n%2==1);
But if you do have LINQ available, keeping the difference between deferred execution and immediate execution is important.

- 5,956
- 2
- 25
- 22
If I recall correctly, the main difference (besides what they're implemented on: IEnumerable<T>
vs. List<T>
) is that Where
implements deferred execution, where it doesn't actually do the lookup until you need it -- using it in a foreach loop for example. FindAll
is an immediate execution method.

- 2,530
- 3
- 31
- 44
I did 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 call. Sometimes it was the same time, other times it was faster.

- 428
- 3
- 12

- 823
- 6
- 3
-
6Did you try accessing the collection as well? Enumerable.Where() uses deferred execution and does not get evaluated before the collection is accessed, which could lead to false conceptions on whether its actually faster or not. Still, it's for the most part, typically faster to use enumerables rather than static collections (like Type
and Array – Sebastian Job Bjørnager Jensen May 14 '14 at 12:33). -
3Question is about FindAll. It is obvious Find will be faster than Where (taking all the values) and getting the FirstOrDefault – Vivek MVK Dec 20 '19 at 17:40
-
-
1Using the Where or FindAll with the .Count() function has almost the same performance but there is a massive difference when FindAll uses the Length property instead of Count() hereby the FindAll is more suitable for search and filtering Arrays. – Adebayo Vicktor Feb 09 '22 at 23:24
Performance wise FindAll()
is better, Here is an example below. The FindAll
takes 3millisecs while the Where
took 11millisecs.
public class SortedSearch
{
public static int[] CountNumbersUsingFindAll(int[] sortedArray, int lessThan)
{
var smaller = Array.FindAll(sortedArray,x => x < lessThan);
return smaller;
}
public static IEnumerable<int> CountNumbersUsingWhere(int[] sortedArray,int lessThan)
{
var smaller = sortedArray.Where(x => x < lessThan);
return smaller;
}
}
class Program
{
static void Main(string[] args)
{
Stopwatch s = Stopwatch.StartNew();
Console.WriteLine(SortedSearch.CountNumbersUsingFindAll(new int[]{1,3,5,7},4));
Console.WriteLine(s.ElapsedMilliseconds);
s.Stop();
s.Restart();
Console.WriteLine(SortedSearch.CountNumbersUsingWhere(new int[] { 1, 3, 5, 7 }, 4));
Console.WriteLine(s.ElapsedMilliseconds);
s.Stop();
Console.ReadKey();
}
}

- 79
- 3
-
1What kind of environment are you in where the method running through an array of 4 integers takes anything close to 3 seconds, much less 11? Something's not right with those results. They seem off by multiple orders of magnitude. Was it perhaps 3 milliseconds vs 11 milliseconds? If that's the case, I suggest not including the WriteLine statements in the timing. – Mark Meuer Mar 07 '22 at 23:06
-