[C#] Linq常用语法

<strong>Linq常用语法</strong>

 Student[] student = {            new Student{ID=1, Name="小黑",Age=3,Class=1},
                                  new Student{ID=2,Name="小布",Age=5,Class=1},
                                  new Student{ID=3,Name="朱迪",Age=20,Class=2},
                                  new Student{ID=4,Name="狐尼克",Age=21,Class=2}
                      };
StudentClass[] studentClass = { new StudentClass{ID=1, Name="一班"},
                                new StudentClass{ID=2, Name="二班"}
                                };

1 简单的linq语法

 //1 简单的linq语法
            var s11 = from r in student
                      select r;
            var s12 = student;
            //"select * from student";

2.linq where 查询

  var s21 = from r in student
                      where r.ID > 2
                      select r;
            var s22 = student.Where(p => p.ID > 2);
            //"select * from student where ID>2";

3.linq 简单的函数计算(count,min,max,sum)

           //获取最大的ID
            var s311 = (from r in student
                        select r).Max(p => p.ID);
            //获取最小的ID
            var s312 = (from r in student
                        select r).Min(p => p.ID);
            //获取结果集的总数
            var s313 = (from r in student
                        select r).Count();
            //获取ID的和
            var s314 = (from r in student
                        select r).Sum(p => p.ID);

            var s321 = student.Max(p => p.ID);
            var s322 = student.Min(p => p.ID);
            var s323 = student.Count();
            var s324 = student.Sum(p => p.ID);

            // "select max(ID) from student";
            // "select min(ID) from studente";
            // "select count(1) from student";
            // "select sum(ID) from student";

4.linq 排序order by desc/asc

            var s41 = from r in student
                     where r.ID > 1
                      orderby r.ID descending  //倒序
                      //orderby r.ID ascending   //正序
                     select r;

            //正序
            var s42 = student.OrderBy(p => p.ID).Where(p => p.ID > 1).ToList();
            //倒序
            var s43 = student.OrderByDescending(p => p.ID).Where(p => p.ID > 1).ToList();

            // "select * from student where ID>1 order by ID [desc|asc]";

5.linq top

 //如果取最后一个可以按倒叙排列再取值
            var s51 = (from r in student                     
                      select r).FirstOrDefault();
            //()linq to ef 好像不支持 Last() 
            var s52 = student.FirstOrDefault();
            var s53 = student.First();
            // "select top(1) * from student";

6.linq 跳过前面多少条数据取余下的数据

var s61 = (from r in student
                      orderby r.ID descending
                      select r).Skip(10); //跳过前10条数据,取10条之后的所有数据   
            var s62 = student.OrderByDescending(p => p.ID).Skip(10).ToList();
// "select * from  (select ROW_NUMBER()over(order by ID desc) as rowNum, * from [student]) as t where rowNum>10";

7.linq 分页数据查询

 var s71 = (from r in student
                      where r.ID > 10
                      orderby r.ID descending
                      select r).Skip(10).Take(10); //取第11条到第20条数据                   

            //Take(10): 数据从开始获取,获取指定数量(10)的连续数据
            var s72 = student.OrderByDescending(p => p.ID).Where(p => p.ID > 10).Skip(10).Take(10).ToList();
            //"select * from  (select ROW_NUMBER()over(order by ID desc) as rowNum, * from [student]) as t 
            // where rowNum>10 and rowNum<=20";

8.linq 包含,类似like '%%'

 var s81 = from r in student
                     where r.Name.Contains("小")
                     select r;
            var s82 = student.Where(p => p.Name.Contains("小")).ToList();
            // "select * from student where Name like '%小%'";

9.linq 分组group by

 var s91 = from r in student
                     orderby r.ID descending
                     group r by r.Class into n
                     select new
                     {
                         n.Key,  //这个Key是recType
                         rpId = n.Sum(r => r.ID), //组内rpId之和
                         MaxRpId = n.Max(r => r.ID),//组内最大rpId
                         MinRpId = n.Min(r => r.ID), //组内最小rpId
                     };
            foreach (var t in s91)
            {
                Console.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId);
            }
            //2
            var s92 = from r in student
                      orderby r.ID descending
                      group r by r.Class into n
                      select n;
            foreach (var t in s92)
            {
                Console.Write(t.Key + "--" + t.Min(p => p.ID));
            }
            //3
            var s93 = student.GroupBy(p => p.Class);
            foreach (var t in s93)
            {
                Console.Write(t.Key + "--" + t.Min(p => p.ID));
            }
            //"select Class,min(ID),max(ID),sum(ID) from student group by Class";

10.linq 连接查询 

  //1
            var s101 = from r in student
                     join w in studentClass on r.Class equals w.ID
                     orderby r.ID descending
                     select r;
            //2
var s102 = student.Join(studentClass, p => p.Class, r => r.ID, (p, r) => p).OrderByDescending(p => p.ID).ToList();
            //3
            // "select r.* from  student as r 
            //               inner join [dbo].[studentClass] as t 
            //                           on r.[Class] = t.[ID] 
            //order by r.[ID] desc";

11.linq 中的In

  var s111 = from p in student
                     where (new int?[] { 2,3,4 }).Contains(p.ID)
                     select p;
            foreach (var p in s111)
            {
                Console.Write(p.Name);
            }
            //2
            //"select * from student where ID in(2,3,4)";


参考原文:http://www.cnblogs.com/knowledgesea/p/3897665.html
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章