I have a list like this:
List people
age name
1 bob
1 sam
7 fred
7 tom
8 sally
I need to do a linq query on people and get an int of the number distinct ages (3)
int distinctAges = people.SomeLinq();
how? how?
Select out the age, then use Distinct and Count.
var ages = people.Select( p => p.Age ).Distinct().Count()
Or you could use GroupBy and Count
var ages = people.GroupBy( p => p.Age ).Count();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With