Try the following:
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 have not tested above, but you can check your source code in case something happened in the rewriting:
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);
source share