Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get item count of a list<> using Linq

Tags:

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; 
like image 612
Geeth Avatar asked Oct 04 '10 05:10

Geeth


People also ask

How do you count in LINQ?

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.

How can I count the occurrences of a list item C #?

Use the list. count() method of the built-in list class to get the number of occurrences of an item in the given list.

What is .count in C#?

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.

What is any () in LINQ?

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.


1 Answers

 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.

like image 105
Ani Avatar answered Oct 08 '22 09:10

Ani