Ian Vink
Ian Vink

Reputation: 68840

C# Linq totals of children collections

I have a Country object which has States. Each state has a Persons collection. (Simplified, but the structure is the same)

I am trying to find out how many people in the country have person.IsAlive==true

I'm trying something like

  country.States.SelectMany(e=>e.Persons...).Count 

but loose it there.

Items?

Upvotes: 0

Views: 41

Answers (1)

Grant Winney
Grant Winney

Reputation: 66511

You're really close. Select the IsAlive property, and perform a Count on the matching records:

country.States.SelectMany(e => e.Persons.Where(p => p.IsAlive)).Count();

Upvotes: 3

Related Questions