码迷,mamicode.com
首页 > 其他好文 > 详细

LINQ语法记录

时间:2016-03-20 13:07:22      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

  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();
        }
    }

 

LINQ语法记录

标签:

原文地址:http://www.cnblogs.com/LQBlog/p/5297435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!