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();
精彩评论