开发者

Nested lambda expression

开发者 https://www.devze.com 2023-03-06 00:37 出处:网络
How can I get the Count of Person whose name equals开发者_JAVA技巧 \"john\" from a List using lambda expressions.

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

精彩评论

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

关注公众号