Reputation: 4657
I have a generic list of int List<int>()
and I want to know Count of a item of it. How can I do it?
Upvotes: 3
Views: 25189
Reputation: 10125
Count of A Generic List (generated from stored procedure)
List<GetDivisionsForGroup> GroupDivision = new List<GetDivisionsForGroup>();
String storedProcedures = "Sp_GetDivisionsForGroup " + grpid;
GroupDivision = db.ExecuteStoreQuery<GetDivisionsForGroup>("exec " + storedProcedures).ToList();
if (GroupDivision.Count > 0)
{
}
Upvotes: 1
Reputation: 251042
If you want to get a count for the number of items in the list you can use an expression to do this:
int count = myList.Count(i => i.Equals(5));
Upvotes: 6
Reputation: 4902
using the extension method is the simplest way
int count = list.Count(i => i > 10);// get the count of those which is bigger than 10
Upvotes: 13
Reputation: 5474
http://code.msdn.microsoft.com/LINQ-Aggregate-Operators-c51b3869
public void Linq73()
{
int[] factorsOf300 = { 2, 2, 3, 5, 5 };
int uniqueFactors = factorsOf300.Distinct().Count();
Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
}
Upvotes: 1