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 ?
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With