I have a collection of person objects (IEnumerable) and each person has an age property.
I want to generate stats on the collection such as Max, Min, Average, Median, etc on this age property.
What is the most elegant way of doing this using LINQ?
var max = persons.Max(p => p.age);
var min = persons.Min(p => p.age);
var average = persons.Average(p => p.age);
Fix for median in case of even number of elements
int count = persons.Count();
var orderedPersons = persons.OrderBy(p => p.age);
float median = orderedPersons.ElementAt(count/2).age + orderedPersons.ElementAt((count-1)/2).age;
median /= 2;