Max :
var biggestGrpOfPeopleHavingSamePersonId =
people.GroupBy(x => x.personid).Max(x => x.Count());
Top :
var top3peopleGroups =
people.GroupBy(x => x.personid).OrderByDescending(x => x.Count()).Take(3);
EDIT :
The first query returns an element of type IGrouping<TKey,TValue>
where TKey
is of the same type of personid
and TValue
is of type Person
. The second query returns an IEnumerable<>
of objects like that.
So, for example in the second case you can do:
foreach(var g in top3peopleGroups)
{
Console.WriteLine("Person Id: " + g.Key + ", Count: " + g.Count());
}
4
solved LINQ: Howto get person count by personid [closed]