This is an already asked question, but that question is to work with 2 attributes only, I need to work with 3 attributes, So I am copy-pasting most of the text.
Let's suppose if we have a class like
class Person {
internal int PersonID;
internal string car ;
internal string friend ;
}
Now I have a list of this class: List persons;
Now this list can have instances multiple same PersonIDs, for ex.
persons[0] = new Person { PersonID = 1, car = "Ferrari" , friend = "Josh" };
persons[1] = new Person { PersonID = 1, car = "BMW" , friend = "Olof" };
persons[2] = new Person { PersonID = 2, car = "Audi" , friend = "Gustaf" };
Is there a way I can group by PersonID and get the list of all the cars and friends he has? For example the expected result would be:
class Result {
int PersonID;
List<string> cars;
List<string> friends;
}
From what I have done so far:
IEnumerable resultsForDisplay = ResultFromSQL_Query.GroupBy(
p => p.PersonId.ToString(),
p => p.car,
(key, g) => new { PersonId = key, car = g.ToList()});
But now I'm stuck at getting the friend's
array in resultsForDisplay