static void Main(string[] args) { List<Person> persons = new List<Person>(); persons.Add(new Person() { Age = 11, ClassId = "1", Name = "小明" }); persons.Add(new Person() { Age = 15, ClassId = "2", Name = "小明2" }); persons.Add(new Person() { Age = 16, ClassId = "5", Name = "小明3" }); persons.Add(new Person() { Age = 17, ClassId = "4", Name = "小明4" }); persons.Add(new Person() { Age = 18, ClassId = "3", Name = "小明5" }); persons.Add(new Person() { Age = 17, ClassId = "4", Name = "小明6" }); List<Classes> classes = new List<Classes>(); classes.Add(new Classes() { ClassId = "1", ClassName = "1年级" }); classes.Add(new Classes() { ClassId = "2", ClassName = "2年级" }); classes.Add(new Classes() { ClassId = "3", ClassName = "3年级" }); classes.Add(new Classes() { ClassId = "4", ClassName = "4年级" }); classes.Add(new Classes() { ClassId = "5", ClassName = "5年级" }); //查询年纪大于12 小于17的学生 List<Person> person = (from p in persons where p.Age > 12 && p.Age < 17 select p).ToList<Person>(); //表连接 var datas = from p in persons join c in classes on p.ClassId equals c.ClassId select new { p.Name, p.Age, c.ClassName }; //查询每个班级每个年龄段的人数 并排序 var data = from c in classes join p in persons on c.ClassId equals p.ClassId group c by new {c.ClassName,p.Age} into g orderby g.Count() descending select new { g.Key,Count = g.Count() } ; Console.ReadKey(); } }
时间: 2024-10-11 23:08:02