title: How do you flatten a Linq query after using double grouping?
body: <p>Suppose I have a class representing order lines , eg</p>
  
  <pre><code>public class Line
  {
      public string Code ;
      public string No ; // Invoice Number
      public DateTime Date ;
      public string Product ;
      public decimal Quantity ;
  }
  </code></pre>
  
  <p>and a List of lines, eg </p>
  
  <pre><code>List&lt;Line&gt; myList = new List&lt;Line&gt;();
  myList.Add(new Line() { Code = "ABC001", No = "1001" ,Date = new DateTime(2012,4,1) ,  Product = "X", Quantity= 1m});
  myList.Add(new Line() { Code = "ABC001", No = "1001" ,Date = new DateTime(2012,4,1) ,  Product = "Y", Quantity= 1m});
  
  myList.Add(new Line() { Code = "ABC002", No = "1002" ,Date = new DateTime(2012,4,2) ,  Product = "X", Quantity= 1m});
  myList.Add(new Line() { Code = "ABC002", No = "1002" ,Date = new DateTime(2012,4,2) ,  Product = "Y", Quantity= 1m});
  myList.Add(new Line() { Code = "ABC002", No = "1003" ,Date = new DateTime(2012,4,3) ,  Product = "Z", Quantity= 1m});
  myList.Add(new Line() { Code = "ABC002", No = "1004" ,Date = new DateTime(2012,4,4) ,  Product = "X", Quantity= 1m});
  
  myList.Add(new Line() { Code = "ABC003", No = "1005" ,Date = new DateTime(2012,4,4) ,  Product = "X", Quantity= 1m});
  myList.Add(new Line() { Code = "ABC003", No = "1006" ,Date = new DateTime(2012,4,4) ,  Product = "X", Quantity= 1m});
  myList.Add(new Line() { Code = "ABC003", No = "1006" ,Date = new DateTime(2012,4,4) ,  Product = "Y", Quantity= 1m});
  </code></pre>
  
  <p>I am looking to retrieve all lines where the Customer Code has more than one invoice.
  To do this, I am first of all grouping by the Code, No and Date and then grouping that by customer Code and for any customers which two or more records, I am selecting all but the first record.</p>
  
  <p>Like so:</p>
  
  <pre><code>var query1 = 
      (from r in myList
          group r by new { r.Code ,  r.No , r.Date } into results
          group results by new { results.Key.Code } into results2 
          where results2.Count() &gt; 1
          select new 
          {   
              results2.Key.Code , 
              Count = results2.Count(), 
              Results = results2.OrderBy(i=&gt;i.Key.Date).Skip(1).ToList() 
            // Skip the first invoice
          } 
       ).ToList();
  </code></pre>
  
  <p>query1 now contains the correct records, but wrapped inside IGrouping and I am having trouble gets the results out as a <code>List&lt;Line&gt;</code></p>
  
  <p>I tried 
      query1.SelectMany(r=>r.Results).ToList(); </p>
  
  <p>but this still leaves me with IGrouping and that's where I am stuck.</p>
  
  <p>I could resort to the nested for loops as in</p>
  
  <pre><code>List&lt;Line&gt; output = new List&lt;Line&gt;();
  foreach (var r1 in query1)
  {
      foreach(var r2 in r1.Results)
          foreach(var r3 in r2)
              output.Add(r3);     
  }
  </code></pre>
  
  <p>but is there a better/Linq way?</p>
  
  <p>The actual output should be four lines as in</p>
  
  <pre><code>// Code    No    Date              Product Quantity 
  // ABC002 1003 03/04/2012 00:00:00 Z 1 
  // ABC002 1004 04/04/2012 00:00:00 X 1 
  // ABC003 1006 04/04/2012 00:00:00 X 1 
  // ABC003 1006 04/04/2012 00:00:00 Y 1 
  </code></pre>