title: How to Group Multiple Items using GroupBy()?
body: <p>Consider the following simplified objects and relationships:</p>
  
  <pre><code>public class Job{
    int JobId;
    String name;
    String status;
    Discipline disc;
    WorkCategory workCat;
  }
  
  public class Discipline {
    int DisciplineId;
    String DisciplineName;
  }
  
  public class Workcategory{
     int WorkCategoryId;
     String WorkCategoryName;
  }
  
  public Class Grouping{
     Discipline parent;
     List&lt;Workcategory&gt; children;
  }
  </code></pre>
  
  <p>The above models a relationship that a <code>Job</code> is associated with one <code>Discipline</code> and one <code>WorkCategory</code>. The <code>Workcategory</code> is always a child of a specific parent <code>Discipline</code> (one-to-many relationship). We can assume that the relationship of the assigned Discipline and Workcategory will always be valid.</p>
  
  <p>The issue I am facing is when I am trying to create a <code>Grouping</code> result object based on filters that are applied to <code>Jobs</code>. I am not sure If this can be done or if the approach I am taking is even correct. The exact question itself is not clear to me, however the above defines the problem statement.</p>
  
  <ol>
  <li>Can the design be improved? </li>
  <li>How can I group Jobs by Discipline and Workcategory?</li>
  <li>Do I even need the <code>Grouping</code> class?</li>
  </ol>
  
  <p>I have tried the following (this is my first attempt using Linq), but to no success as my understanding is not complete enough. The other alternative is to first get the <code>Discipline</code> group and loop through the original grouping picking up the related <code>Workcategory</code>.</p>
  
  <pre><code>var grouping = repository.GetJobsWithActiveStatus()
              .GroupBy(x =&gt; new {
                                    x.Discipline.DisciplineID, 
                                    x.Discipline.DisciplineName,  
                                    x.Category.WorkCategoryID, 
                                    x.Category.WorkCategoryName
                                })
              .Select(g =&gt; new Grouping{
                                   Discipline = new Discipline{
                                                    DisciplineID = g.Key.DisciplineID, 
                                                    Name = g.Key.DisciplineName
                                                    }, 
                                   Categories = ?? // this is where I am lost
                               }});
  </code></pre>
  
  <p>Edit:
  After having posted this, I realized that the inital GroupBy parameters result in one group of item whereas I am looking for Group-SubGroup result.</p>
  
  <p>Edit 2:
  To clarify a bit further, I dont want the associated Jobs as part of the result, but rather the Discipline-Workcategory grouping - thus the reason for the <code>Grouping</code> class</p>
  
  <hr>
  
  <p><strong>Initial solution based on @Obalix</strong></p>
  
  <p><strong><em>Edit 7-Mar-2010:</em></strong> </p>
  
  <p>This solution does not work - The GroupBy on the object Discipline will yield a unique grouping for each object. I think this is due to it being a reference type. Am I correct?
  I initially accepted this as the answer, however after some head scratching realised that my mock data itself was faulty. The initial questions still remain answered.</p>
  
  <pre><code>var result = repository.GetJobsWithActiveStatus()
        .GroupBy(x =&gt; x.disc)
        .Select(g =&gt; new Grouping
                {
                    Discipline = g.Key,
                    Catergories = g.GroupBy(x=&gt;x.workCat) // &lt;--- The Problem is here, grouping on a reference type
                                   .Select(l=&gt;l.Key) // needed to select the Key's only
                                   .ToList()
                });
  </code></pre>