title: Convert flat db row data to nested typed objects linq
body: <p>I'm getting the results of a sql outer join as flat results in an IEnumerable, and would like to convert them to nested typed objects in linq. From something like this:</p>
  
  <pre><code>[{id: 1, industryId: 1}, {id:1, industryId: 2}, {id:2, industryId: 1} etc..]
  </code></pre>
  
  <p>to something like this: </p>
  
  <pre><code>list of Company [{id: 1, list of Industry{industryId: 1, 2}, {id: 2, list of Industry{industryId: 1}}]
  </code></pre>
  
  <p>I'm currently trying a solution with GroupBy:</p>
  
  <pre><code>Companies = flatDbRows
                  .GroupBy(
                  row =&gt; row.CompanyId,
                  (key, value) =&gt; new CompanyModel
                  {
                      CompanyId = value.First().CompanyId,
                      CompanyName = value.First().CompanyName,
                      Industries = value
                          .GroupBy(
                              row =&gt; new { row.IndustryId, row.Industry },
                              (k, v) =&gt; new IndustryModel() { IndustryId = k.IndustryId, Name = k.Industry }
                          )
                          .Where(x =&gt; x.IndustryId != 0)
                          .ToList(),
                  }).ToList();
          }
  </code></pre>
  
  <p>but it doesn't feel great, especially with all the value.First() I'm using to get the values that only belong to each grouped company. Is there something more appropriate? Group join sounded more like what I wanted, but I'm having trouble understanding how to apply it to a single list. I'm open to using query syntax instead of the lambdas if that's easier. </p>
  
  <p>I'm trying to go from this model (where company-related info will be duplicated for each outer joined industry result):</p>
  
  <pre><code>public class CompanyFlatDbRowsModel
  {
      public int CompanyId { get; set; }
      public string CompanyName { get; set; }
      public int IndustryId{ get; set; }
      public string Industry { get; set; }
  }
  </code></pre>
  
  <p>to this: </p>
  
  <pre><code>public class CompanyModel
  {
      public int CompanyId { get; set; }
      public string CompanyName { get; set; }
      public IEnumerable&lt;IndustryModel&gt; Industries { get; set; }
  }
  </code></pre>