title: Linq group by parent property order by child
body: <p>Basicly what i am trying to do is getting the parent grouped by Parent.type where their Child.type has status 'y' ordered by child.Date</p>
  
  <p>As an example i created these classes:</p>
  
  <pre><code>public class Collection
  {
      public IEnumerable&lt;Parent&gt; Parents { get; set; }
      public int Id { get; set; }
  }
  
  public class Parent
  {
      public int Id { get; set; }
      public Type type { get; set; }       
      public IEnumerable&lt;Child&gt; Children { get; set; }
  }
  
  public class Child
  {
      public int Id { get; set; }
      public Status Status { get; set; }
      public DateTime Date { get; set; }
  }
  
  public enum Type
  {
      a,
      b
  }
  
  public enum Status
  {
      x,
      y
  }
  </code></pre>
  
  <p>What i want is a list of Parents of each type where the Child's Date is highest in value.</p>
  
  <p>To get this example going i added some test data:</p>
  
  <pre><code>Collection collection= new Collection
          {
              Id = 1,
              Parents = new List&lt;Parent&gt;
              {
                  new Parent
                  {
                      Id= 1,
                      type = Type.a,
                      Children = new List&lt;Child&gt;
                      {
                          new Child
                          {
                              Id = 1,
                              Status = Status.y,
                              Date = DateTime.Now.AddDays(-1)
                          },
                          new Child
                          {
                              Id = 2,
                              Status = Status.x,
                              Date = DateTime.Now.AddDays(-1)
                          }
                      }
                  },
                  new Parent
                  {
                      Id= 2,
                      type = Type.b,
                      Children = new List&lt;Child&gt;
                      {
                          new Child
                          {
                              Id = 3,
                              Status = Status.y,
                              Date = DateTime.Now.AddDays(-2)
                          },
                          new Child
                          {
                              Id = 4,
                              Status = Status.x,
                              Date = DateTime.Now.AddDays(-2)
                          }
                      }
                  },
                  new Parent
                  {
                      Id= 3,
                      type = Type.a,
                      Children = new List&lt;Child&gt;
                      {
                          new Child
                          {
                              Id = 5,
                              Status = Status.y,
                              Date = DateTime.Now.AddDays(-3)
                          }
                      }
                  },
                  new Parent
                  {
                      Id= 4,
                      type = Type.b,
                      Children = new List&lt;Child&gt;
                      {
                          new Child
                          {
                              Id = 6,
                              Status = Status.y,
                              Date = DateTime.Now.AddDays(-3)
                          },
                          new Child
                          {
                              Id = 7,
                              Status = Status.y,
                              Date = DateTime.Now.AddDays(-4)
                          },
                          new Child
                          {
                              Id = 8,
                              Status = Status.x,
                              Date = DateTime.Now.AddDays(-4)
                          }
                      }
                  },
              }
          };
  </code></pre>
  
  <p>The result of the select should be a List containing 2 Parents. One for each type ( a and b ). Both of these still containing all of their Child objects.</p>
  
  <p>Is there any simple solution for this ?</p>
  
  <p>I tried something like:</p>
  
  <pre><code>List&lt;Parent&gt; test = collection.Parents
                  .GroupBy(m =&gt; m.type)
                  .Select(
                  m =&gt; m.OrderByDescending(
                      mm =&gt; mm.Children.Select(
                          r =&gt; r).OrderByDescending(
                              r =&gt; r.Date)).FirstOrDefault()).ToList();
  </code></pre>
  
  <p>But that didn't end well.</p>
  
  <p>=====UPDATE=====</p>
  
  <p>Thanks to the example of Enigmativity this issue is solved. I made a slight modification to the linq query as the original content i am using is provided by Entity Framework (6). This meant for me that the selected Parent object was not containing any Children, so i had to select into a new object type (As we can't instantiate a new object of the type provided by the Entity Framework model) . Also my Parents were in another IEnumerable so I had to use a SelectMany on the Parents as well.</p>
  
  <p>This resulted in something like this: (This won't work with the test classes though)</p>
  
  <pre><code>var result =
      collection
          .SelectMany(c =&gt; c.Parents)
          .GroupBy(p =&gt; p.type)
          .SelectMany(gps =&gt;
              gps
                  .SelectMany(
                      gp =&gt; gp.Children.Where(c =&gt; c.Status == Status.y),
                      (gp, c) =&gt; new { gp, c })
                  .OrderByDescending(gpc =&gt; gpc.c.Date)
                  .Take(1)
                  .Select(gpc =&gt; new ParentData {Id = gpc.gp.Id, Children= gpc.gp.Children}))
          .ToList();
  </code></pre>