Suppose that we have this class:
public enum KindOfPerson
{
Student, Teacher, ...
}
public class Person
{
// This contains only numbers between 0 and 1
public double ScorePercent { get; set; }
public KindOfPerson Type { get; set; }
}
I’m trying to classify a list by KindOfPerson, but also I’d like to know what is the average of ScorePercent of all the people of the same KindOfPerson.
List<List<Person>> groupedLists = peopleList.GroupBy(person => person.Type)
.OrderBy(group => group.Key)
.Select(group => group.ToList())
.ToList();
Do you want to do it with LINQ?
You could use
GroupByandAveragemethods.Update
Assuming you want a list of
Tuple<KindOfPerson, double>, here you go:Creating a
Dictionary<KindOfPerson, double>is very similar: