public static void Demo2() { var methodFormat = GroupByStudent.GetStudents() .GroupBy(std => std.Gender) .OrderByDescending(g => g.Key) .Select(std => new { Key = std.Key, Students = std.OrderBy(s => s.Name) }); var queryFormat = from std in GroupByStudent.GetStudents() group std by std.Gender into genderGroup orderby genderGroup.Key descending select new { Key = genderGroup.Key, Students = genderGroup.OrderBy(s => s.Name) }; foreach (var group in methodFormat) { Console.WriteLine(group.Key + " : " + group.Students.Count()); //Iterate through each student of a group foreach (var student in group.Students) { Console.WriteLine(" Name :" + student.Name + ", Age: " + student.Age + ", Branch :" + student.Barnch); } } }
public static void Demo1() { var methodFormat = GroupByStudent.GetStudents() .GroupBy(std => new { std.Barnch, std.Gender }) .OrderByDescending(g => g.Key.Barnch) .ThenBy(g => g.Key.Gender) .Select(stdGroup => new { Branch = stdGroup.Key.Barnch, Gender = stdGroup.Key.Gender, Students = stdGroup.OrderBy(x => x.Name) }); var queryFormat = from std in GroupByStudent.GetStudents() group std by new { std.Barnch, std.Gender } into stdGroup select new { Branch = stdGroup.Key.Barnch, Gender = stdGroup.Key.Gender, Students = stdGroup.OrderBy(x => x.Name) }; foreach (var group in methodFormat) { Console.WriteLine($"Barnch : {group.Branch} Gender: {group.Gender} No of Students = {group.Students.Count()}"); //It will iterate through each item of a group foreach (var student in group.Students) { Console.WriteLine($" ID: {student.ID}, Name: {student.Name}, Age: {student.Age} "); } Console.WriteLine(); } }
public static void Demo1() { var methodFormat = GroupByStudent.GetStudents().ToLookup(s => s.Barnch); var queryFormat = (from std in GroupByStudent.GetStudents() select std).ToLookup(s => s.Barnch); foreach (var group in methodFormat) { Console.WriteLine(group.Key + " : " + group.Count()); //Iterate through each student of a group foreach (var student in group) { Console.WriteLine(" Name :" + student.Name + ", Age: " + student.Age + ", Gender :" + student.Gender); } } }