For list of objects of class Animal
public class Animal
{
public int id { get; set; }
public string name{ get; set; }
public string category { get; set; }
public int avg_lifespan { get; set; }
}
How can I return the animals with the highest avg_lifespan by category?
e.g. I have a dataset with 50 animals within 5 categories, I want to return, for each category, the animal with the highest average lifespan.
So far, I have this LINQ statement that returns the distinct categories:
var categories = animals.SelectMany(e => e.category).Distinct();
My idea was to iterate through the list of Animal objects, and for each animal in the list, increment some value with the corresponding animal's average lifespan. How can I do this more elegantly ?