Ahmadali Shafiee
Ahmadali Shafiee

Reputation: 4657

Get Count of an Item of Generic List

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

Answers (4)

Arun Prasad E S
Arun Prasad E S

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

Fenton
Fenton

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

ojlovecd
ojlovecd

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

John K.
John K.

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

Related Questions