title: Nested Groups in LINQ with Entity Framework
body: <p>I have query below, which groups students result data by <code>class</code>, <code>group</code>, <code>enrollment</code> as shown in code</p>
  
  <pre><code>public List&lt;PassingRatioDTO&gt; GetStudentPassingRatio()
  {
      using(SMSEntities em=new SMSEntities())
      {
          try
          {
              List&lt;PassingRatioDTO&gt; result = (from results in em.ResultDetails
                      where results.ExamDate.Year == 2016
                      group results by new {results.Class_Id, results.Group_Id, results.EnrollmentId } into groupedRresult
                      select new PassingRatioDTO()
                      {
                          Percentage = (((groupedRresult.Where(s =&gt; s.Status == "Pass").Count()) * 100) / groupedRresult.Count()),
                          ClassName = groupedRresult.Where(s =&gt; s.Class_Id == s.ClassDetail.Class_Id).Max(s =&gt; s.ClassDetail.ClassName),
                          GroupName = groupedRresult.Where(s =&gt; s.Group_Id == s.GroupDetail.Group_Id).Max(s =&gt; s.GroupDetail.Group_Name),
                          Year = groupedRresult.Max(s =&gt; s.ExamDate.Year),
                      }
              ).ToList();
  
              return result;
          }
          catch(Exception ex)
          {
              return null;
          }
     }
  </code></pre>
  
  <p>I want to group this query result again and count Percentage (which is a derived column) where percentage is greater then 40..</p>