2011-05-16 217 views
0

如何从列表中使用lambda表达式获取名称等于“john”的Person的Count。 如何创建我的lambda表达式?嵌套的lambda表达式

List<Persons> persons; 
person.Where(p=>p.Name.Equals("John"); 

现在我应该对返回的列表进行计数,还是应该嵌套它?

回答

5

都不是。使用Count方法的过载需要一个表达式:

int cnt = person.Count(p => p.Name.Equals("John")); 
2
person.Where(p=>p.Name.Equals("John")).Count(); 
1
List<Person> persons; 
/* code that populates persons list */ 
int count = persons.Where(p=>p.Name.Equals("John")).Count();