I want to query a List<> and find out how MANY items match the selection criteria. using LINQ and c# /.net 3.5. How would I modify the query to return an int count.
var specialBook = from n in StoreDisplayTypeList where n.DisplayType=="Special Book" select n;
In LINQ, you can count the total number of elements present in the given sequence by using the Count Method. This method returns the total number of elements present in the given sequence.
Use the list. count() method of the built-in list class to get the number of occurrences of an item in the given list.
To count the number of elements in the C# array, we can use the count() method from the IEnumerable. It is included in the System. Linq. Enumerable class. The count method can be used with any type of collection such as an array, ArrayList, List, Dictionary, etc.
The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.
var numSpecialBooks = StoreDisplayTypeList.Count(n => n.DisplayType == "Special Book");
This uses an overload of Enumerable.Count
that takes aFunc<TSource, bool>
predicate to filter the sequence.
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