2011-11-01 23 views
3

我有一个int List<int>()的通用列表,我想知道它的一个项目的计数。我该怎么做?获得通用列表项的计数

+1

你能更具体吗?你的'List '中有哪些特殊项目? –

+0

编辑问题 –

回答

13

是最简单的way

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

它起作用了。但我想这样:list.Count(i => i == 10);非常感谢你 –

+0

@ahmadalishafiee所以你需要的是'int count = myList.Count(i => i.Equals(10));'' ' – Fenton

+0

@Sohnee:我尝试但不起作用 –

6

如果你想获得列表中的项目数的数,您可以使用表达式来做到这一点:使用扩展方法

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

我尝试过,但是什么是i.equals ???我是int并且int没有等于 –

+1

'Equals'是int上的一个方法 - 它需要是一个大写字母E. – Fenton

0

通用列表的计数(从存储过程生成)

List<GetDivisionsForGroup> GroupDivision = new List<GetDivisionsForGroup>(); 
     String storedProcedures = "Sp_GetDivisionsForGroup " + grpid; 
     GroupDivision = db.ExecuteStoreQuery<GetDivisionsForGroup>("exec " + storedProcedures).ToList(); 

     if (GroupDivision.Count > 0) 
     { 

     }