1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace study 7 { 8 class LinqGroup 9 { 10 static void Main(string[] args) 11 { 12 List<Employee> empList = new List<Employee> 13 { 14 new Employee {ID = 1, FName = "John", Age = 23, Sex = 'M', Money=100}, 15 new Employee {ID = 2, FName = "Mary", Age = 25, Sex = 'F', Money=200}, 16 new Employee {ID = 3, FName = "Amber", Age = 23, Sex = 'M', Money=300}, 17 new Employee {ID = 4, FName = "Kathy", Age = 25, Sex = 'F', Money=400}, 18 new Employee {ID = 5, FName = "Lena", Age = 27, Sex = 'F', Money=500}, 19 new Employee {ID = 6, FName = "Bill", Age = 28, Sex = 'M', Money=600}, 20 new Employee {ID = 7, FName = "Celina", Age = 27, Sex = 'F', Money=700}, 21 new Employee {ID = 8, FName = "John", Age = 28, Sex = 'M', Money=800} 22 }; 23 24 List<Employee> empGroupByList = (from a in empList 25 group a by new 26 { 27 a.FName, 28 a.Sex 29 } into b 30 orderby b.Key.FName, b.Key.Sex 31 select new Employee 32 { 33 FName = b.Key.FName, 34 Sex=b.Key.Sex, 35 Age = b.Sum(c => c.Age), 36 ID = b.Max(c => c.ID), 37 Money = b.Average(c => c.Money) 38 }).ToList<Employee>(); 39 foreach (var item in empGroupByList) 40 { 41 Console.WriteLine("item.ID = " + item.ID); 42 Console.WriteLine("item.FName = " + item.FName); 43 Console.WriteLine("item.Age = " + item.Age); 44 Console.WriteLine("item.Sex = " + item.Sex); 45 Console.WriteLine("item.Money = " + item.Money); 46 Console.WriteLine("------------------------"); 47 } 48 49 Console.ReadKey(); 50 } 51 } 52 53 public class Employee 54 { 55 public int ID { get; set; } 56 public string FName { get; set; } 57 public int Age { get; set; } 58 public char Sex { get; set; } 59 public decimal Money { get; set; } 60 } 61 }