简体   繁体   English

获取通用列表项的计数

[英]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. 我有一个int List<int>()的通用列表,我想知道它的一个项目的Count。 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 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); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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