Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between FindAll and Select?

Tags:

c#

.net

linq

I used List.Select(condition).Count() and found the result to be inappropriate and then I tried List.FindAll(condition).Count() and it worked fine.

How does List.Select(condition).count work ?

like image 808
Dotnay Tupperson Avatar asked Jan 29 '26 23:01

Dotnay Tupperson


2 Answers

You see different results because

list.Select(condition)

transforms the list into a sequence of True and False values with the length that is always equal to the number of items in the list. If you use Where instead of Select, you will get matching results.

However, a shorter way to get the result is to pass the condition to Count, like this:

var res = list.Count(condition);
like image 80
Sergey Kalinichenko Avatar answered Jan 31 '26 11:01

Sergey Kalinichenko


List.Select Invokes a transform function on each element in the sequence and returns the transformed collection. In general, using this will return the same Count as the original collection.

List.FindAll takes a predicate (similar to List.Where) and so will only return elements matching it, giving a different count from the original.

like image 23
BradleyDotNET Avatar answered Jan 31 '26 13:01

BradleyDotNET



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!