1

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

openshac
  • 4,966
  • 5
  • 46
  • 77
Atif Waqar
  • 89
  • 2
  • 12

1 Answers1

5

Sure, you can perform LINQ queries on the group g as well, like:

IEnumerable<Result> resultsForDisplay = from q in ResultFromSQL_Query
    group q by q.PersonID into g
    select new Result {PersonID = g.Key,cars = g.Select(x => x.car).ToList(), friends = g.Select(x => x.friend).ToList()};

Or with lambda expressions:

IEnumerable<Result> results = persons.GroupBy(x => x.PersonID)
    .Select(g => new Result { PersonID = g.Key, cars = g.Select(x => x.car).ToList(), friends = g.Select(x => x.friend).ToList()};

So you can perform any LINQ query on a group (which thus behaves as an IEnumerable<> on the elements of that grou), like a .Select(..), but also .Sum(..), .Average(..) and other sub queries, aggregates, etc.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Hi, please forgive me, but im a newbie in Linq and Lambda expression. Could you please convert this syntax to lambda expression? – Atif Waqar Jun 15 '17 at 00:24
  • Also, when i try to use your syntax, it gives me an error on `Result` in line `select new Result { ` the error says `The type or namespace name 'Result' could not be found (are you missing a using directive or an assembly reference?)` – Atif Waqar Jun 15 '17 at 00:27
  • @AtifWaqar: yes, these have to be written on a single line I think. – Willem Van Onsem Jun 15 '17 at 00:28
  • @AtifWaqar: `Result` is a class in your question. If you haven't got a proper reference to it then only you can answer why that is. Are you missing a using directive perhaps? Or did you just not put the class in your code and just in the question? – Chris Jun 15 '17 at 00:31
  • 1
    @WillemVanOnsem This is Amazing. I was stuck at this for a long time. Thanks you very much for the solution :) About `Result`, I got it, its the returning data type. – Atif Waqar Jun 15 '17 at 00:44