title: How to group a list using linq c#
body: <p>The scenario I have is as follows: </p>
  
  <p>I have the following data - </p>
  
  <h2>ID, Name, Type, Location, GroupID</h2>
  
  <pre><code>1, samename, Rock, New York, 12
  2, samename, Jazz, Sydney, 12
  3, samename, Rock, Sydney, 12
  4, samename, Jazz, New York, 12    
  5, name3, Opera House, Sydney, 14
  6, name3, Opera House, London, 14
  7, name2, Emirates, London, 13
  </code></pre>
  
  <p>And I would like to output it flattened based on the GroupID like below </p>
  
  <h2>ID, Name, Place, Location, GroupID</h2>
  
  <pre><code>1, samename, {Rock,Jazz}, {New York,Sydney}, 12
  5, name3, Opera House, {Sydney,London}, 14
  7, name2, Emirates, London, 13
  </code></pre>
  
  <p>This was really bad design that I have inherited - and I am trying to make it better.. without breaking the old code. </p>
  
  <p>I believe the answer is something to do with SelectMany - but I can't work out the syntax - I've tried a few different ways. </p>
  
  <p>my attempted solution - without flattening.. </p>
  
  <pre><code>var q3 = Data.Where(b=&gt;b.GroupID != null).GroupBy(x=&gt; new { x.GroupID }, (key, group) =&gt; new 
  { 
    GroupID = key.GroupID,  
    Result =  group.Select(g=&gt; new 
                           {                            
                             Type = g.Type, 
                             Location = g.Location,                                                  
                           }).ToList()
  });
  </code></pre>