开发者

Nested lambda expression

How can I get the Count of Person whose name equals开发者_JAVA技巧 "john" from a List using lambda expressions. How can I create my lambda expression?

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

Now do I do a count on the returned List or should I nest it?


Neither. Use the overload of the Count method that takes an expression:

int cnt = person.Count(p => p.Name.Equals("John"));


person.Where(p=>p.Name.Equals("John")).Count();


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜