简体   繁体   中英

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?

使用扩展方法是最简单的方法

int count = list.Count(i => i > 10);// get the count of those which is bigger than 10

如果要计算列表中项目数的计数,可以使用表达式执行此操作:

int count = myList.Count(i => i.Equals(5));

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)
        {

        }

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); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM