0

How can I get the Count of Person whose name equals "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?

3 Answers 3

5

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

int cnt = person.Count(p => p.Name.Equals("John"));
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.