score:1

Try this:

        var vGroup = from p in ClassRoom
                     group p by new { p.ClassRoomNo, p.TeacherName }
                         into g
                         from i in g
                         select new
                         {
                             i.ClassRoomNo,
                             i.TeacherName,
                             i.ClassRoomTitle,
                             NoSessionsPerTeacher = g.Count()
                         };

        var pGroup = from p in vGroup
                     group p by new { p.ClassRoomNo }
                         into g
                         from i in g
                         select new
                         {
                             i.ClassRoomTitle,
                             NoSessionsPerRoom = g.Count(),
                             i.TeacherName,
                             i.NoSessionsPerTeacher
                         };

        var result = pGroup.OrderBy(p => p.ClassRoomNo).ThenBy(p => p.TeacherName);

I didn't test the above but you can check my original code in case I got something wrong in the rewrite:

        var vGroup = from p in Products
                     group p by new { p.ProductId, p.VariantId }
                         into g
                         from i in g
                         select new
                         {
                             i.ProductId,
                             i.VariantId,
                             VariantCount = g.Count()
                         };

        var pGroup = from p in vGroup
                     group p by new { p.ProductId }
                         into g
                         from i in g
                         select new
                         {
                             i.ProductId,
                             ProductCount = g.Count(),
                             i.VariantId,
                             i.VariantCount
                         };

        var result = pGroup.OrderBy(p => p.ProductId).ThenBy(p => p.VariantId);

score:1

var classRooms = from c in context.ClassRooms
                 group c by new {c.ClassRoomNo} into room
                 select new {
                    Title = room.First().ClassRoomTitle,
                    NoSessions = room.Count(),
                    Teachers = from cr in room
                               group cr by new {cr.TeacherName} into t
                               select new {
                                   Teacher = t.Key,
                                   NoSessions = t.Count()
                               }
                 };

A bit more structured than the posted expected result, but I find that to be better.

You can always use SelectMany if you want to go back to unstructured:

var unstructured = classRooms
   .SelectMany(c=> c.Teachers.Select( t=> new {
      Title = c.Title,
      SessionsPerRoom = c.NoSessions,
      Teacher = t.Teacher,
      SessionsPerTeacher = t.NoSessions
   });

Related Articles