Group by in LINQ

后端 未结 10 1625
悲哀的现实
悲哀的现实 2020-11-21 06:58

Let\'s suppose if we have a class like:

class Person { 
    internal int PersonID; 
    internal string car; 
}

I have a list of this class

10条回答
  •  既然无缘
    2020-11-21 07:30

    First, set your key field. Then include your other fields:

    var results = 
        persons
        .GroupBy(n => n.PersonId)
        .Select(r => new Result {PersonID = r.Key, Cars = r.ToList() })
        .ToList()
    

提交回复
热议问题