linq查询语法和方法-簡單用法

來自:http://www.cnblogs.com/knowledgesea/p/3897665.html

1.简单的linq语法

复制代码
            //1
            var ss = from r in db.Am_recProScheme
                     select r;
            //2
            var ss1 = db.Am_recProScheme;
            //3
            string sssql = "select * from Am_recProScheme";
复制代码

2.带where的查询

复制代码
            //1
            var ss = from r in db.Am_recProScheme
                     where r.rpId > 10
                     select r;
            //2
            var ss1 = db.Am_recProScheme.Where(p => p.rpId > 10);
            //3
            string sssql = "select * from Am_recProScheme where rpid>10";
复制代码

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

复制代码
            //1
            ////获取最大的rpId
            //var ss = (from r in db.Am_recProScheme
            //          select r).Max(p => p.rpId);
            ////获取最小的rpId
            //var ss = (from r in db.Am_recProScheme
            //          select r).Min(p => p.rpId);
            //获取结果集的总数
            //var ss = (from r in db.Am_recProScheme                  
            //         select r).Count();
            //获取rpId的和
            var ss = (from r in db.Am_recProScheme
                      select r).Sum(p => p.rpId);


            //2
            //var ss1 = db.Am_recProScheme.Max(p=>p.rpId);
            //var ss1 = db.Am_recProScheme.Min(p => p.rpId);
            //var ss1 = db.Am_recProScheme.Count() ;
            var ss1 = db.Am_recProScheme.Sum(p => p.rpId);
            Response.Write(ss);

            //3
            string sssql = "select max(rpId) from Am_recProScheme";
                   sssql = "select min(rpId) from Am_recProScheme";
                   sssql = "select count(1) from Am_recProScheme";
                   sssql = "select sum(rpId) from Am_recProScheme";
复制代码

4.排序order by desc/asc

复制代码
            var ss = from r in db.Am_recProScheme
                     where r.rpId > 10
                     orderby r.rpId descending  //倒序
                     //  orderby r.rpId ascending   //正序
                     select r;

            //正序
            var ss1 = db.Am_recProScheme.OrderBy(p => p.rpId).Where(p => p.rpId > 10).ToList();
            //倒序
            var ss2 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).ToList();

            string sssql = "select * from Am_recProScheme where rpid>10 order by rpId [desc|asc]";
复制代码

5.top(1)

复制代码
            //如果取最后一个可以按倒叙排列再取值
            var ss = (from r in db.Am_recProScheme                     
                      select r).FirstOrDefault();

            //()linq to ef 好像不支持 Last() 
            var ss1 = db.Am_recProScheme.FirstOrDefault();
            //var ss1 = db.Am_recProScheme.First();          

            string sssql = "select top(1) * from Am_recProScheme";
复制代码

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

复制代码
            //1
            var ss = (from r in db.Am_recProScheme
                      orderby r.rpId descending
                      select r).Skip(10); //跳过前10条数据,取10条之后的所有数据   
            //2  
            var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Skip(10).ToList();
            //3
            string sssql = "select * from  (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10";
复制代码

7.分页数据查询

复制代码
            //1
            var ss = (from r in db.Am_recProScheme
                      where r.rpId > 10
                      orderby r.rpId descending
                      select r).Skip(10).Take(10); //取第11条到第20条数据                   

            //2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据
            var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).Skip(10).Take(10).ToList();
            //3
            string sssql = "select * from  (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10 and rowNum<=20";
复制代码

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

复制代码
            //1
            var ss = from r in db.Am_recProScheme
                     where r.SortsText.Contains("张")
                     select r;
            //2
            var ss1 = db.Am_recProScheme.Where(p => p.SortsText.Contains("")).ToList();
            //3
            string sssql = "select * from Am_recProScheme where SortsText like '%张%'";
复制代码

9.分组group by

复制代码
            //1
            var ss = from r in db.Am_recProScheme
                     orderby r.rpId descending
                     group r by r.recType into n
                     select new
                     {
                         n.Key,  //这个Key是recType
                         rpId = n.Sum(r => r.rpId), //组内rpId之和
                         MaxRpId = n.Max(r => r.rpId),//组内最大rpId
                         MinRpId = n.Min(r => r.rpId), //组内最小rpId
                     };
            foreach (var t in ss)
            {
                Response.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId);
            }
            //2
            var ss1 = from r in db.Am_recProScheme
                     orderby r.rpId descending
                     group r by r.recType into n
                     select n;
            foreach (var t in ss1)
            {
                Response.Write(t.Key + "--" + t.Min(p => p.rpId));
            }
            //3
            var ss2 = db.Am_recProScheme.GroupBy(p => p.recType);
            foreach (var t in ss2)
            {
                Response.Write(t.Key + "--" + t.Min(p => p.rpId));
            }
            //4
            string sssql = "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";
复制代码

10.连接查询 

复制代码
            //1
            var ss = from r in db.Am_recProScheme
                     join w in db.Am_Test_Result on r.rpId equals w.rsId
                     orderby r.rpId descending
                     select r;
            //2
            var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList();
            //3
            string sssql = "select r.* from  [Am_recProScheme] as r inner join [dbo].[Am_Test_Result] as t on r.[rpId] = t.[rsId] order by r.[rpId] desc";
复制代码

11.sql中的In

复制代码
            //1
            var ss = from p in db.Am_recProScheme
                              where (new int?[] { 24, 25,26 }).Contains(p.rpId)
                              select p;
            foreach (var p in ss)
            {
                Response.Write(p.Sorts);
            }
            //2
            string st = "select * from Am_recProScheme where rpId in(24,25,26)";

1. Select

Select操作符对单个序列或集合中的值进行投影。下面的示例中使用select从序列中返回Employee表的所有列:

  1. using (NorthwindDataContext db=new NorthwindDataContext())  
  2. {  
  3.     //linq查询语法(基本语法)  
  4.     var query =  
  5.         from e in db.Employees  
  6.         where e.FirstName.StartsWith("M")  
  7.         select e;  
  8.     //linq方法语法(基本方法)
  9.     var q =  
  10.         db.Employees  
  11.         .Where(e => e.FirstName.StartsWith("M"))  
  12.         .Select(e => e);  
  13.     foreach (var item in query)  
  14.     {  
  15.         Console.WriteLine(item.FirstName);  
  16.     }  
  17. }  


当然,你也可以返回单个列,例如:

  1. var query =  
  2.         from e in db.Employees  
  3.         where e.FirstName.StartsWith("M")  
  4.         select e.FirstName;  

你也可以返回序列中的某几列,例如:

  1. var query =  
  2.     from e in db.Employees  
  3.     where e.FirstName.StartsWith("M")  
  4.     select new  
  5.     {  
  6.         e.FirstName,  
  7.         e.LastName,  
  8.         e.Title  
  9.     };  

2. SelectMany

SelectMany操作符提供了将多个from子句组合起来的功能,它将每个对象的结果合并成单个序列。下面是一个示例:

  1. using (NorthwindDataContext db=new NorthwindDataContext())  
  2. {  
  3.     //查询语法   
  4.     var query =  
  5.         from e in db.Employees  
  6.         from o in e.Orders  
  7.         select o;  
  8.     //方法语法   
  9.     var q =  
  10.         db.Employees  
  11.         .SelectMany(e => e.Orders);  
  12.     foreach (var item in query)  
  13.     {  
  14.         Console.WriteLine(item.Freight);  
  15.     }  
  16. }  

 

二、限制操作符

Where是限制操作符,它将过滤标准应用在序列上,按照提供的逻辑对序列中的数据进行过滤。

Where操作符不启动查询的执行。当开始对序列进行遍历时查询才开始执行,此时过滤条件将被应用到查询中。Where操作符的使用方法已经在第一节中出现过,这里不再冗述。

三、排序操作符

排序操作符,包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序。

1. OrderBy

OrderBy操作符将序列中的元素按照升序排列。下面的示例演示了这一点:

  1. using (NorthwindDataContext db = new NorthwindDataContext())  
  2. {  
  3.     //查询语法   
  4.     var query =  
  5.         from e in db.Employees  
  6.         orderby e.FirstName  
  7.         select e;  
  8.     //方法语法   
  9.     var q =  
  10.         db.Employees  
  11.         .OrderBy(e => e.FirstName)  
  12.         .Select(e => e);  
  13.     foreach (var item in q)  
  14.     {  
  15.         Console.WriteLine(item.FirstName);  
  16.     }  
  17. }  

这里可以使用OrderBy的重载方法OrderBy(Func,IComparer)来指定序列的排序方式。

2. OrderByDescending

OrderByDescending操作符将序列中的元素按照降序排列。用法与OrderBy相同,这里不再演示。

3. ThenBy

ThenBy操作符实现按照次关键字对序列进行升序排列。此操作符的查询语法与方法语法略有不同,以下代码演示了这一点:

  1. using (NorthwindDataContext db = new NorthwindDataContext())  
  2. {  
  3.     //查询语法   
  4.     var query =  
  5.         from e in db.Employees  
  6.         orderby e.FirstName,e.LastName  
  7.         select e;  
  8.     //方法语法   
  9.     var q =  
  10.         db.Employees  
  11.         .OrderBy(e => e.FirstName)  
  12.         .ThenBy(e => e.LastName)  
  13.         .Select(e => e);  
  14.     foreach (var item in query)  
  15.     {  
  16.         Console.WriteLine(item.FirstName);  
  17.     }  
  18. }  

 

4. ThenByDescending

ThenByDescending操作符实现按照次关键字对序列进行降序排列。此操作符的查询语法与方法语法略有不同,以下代码演示了这一点:

  1. using (NorthwindDataContext db = new NorthwindDataContext())  
  2. {  
  3.     //查询语法   
  4.     var query =  
  5.         from e in db.Employees  
  6.         orderby e.FirstName,e.LastName descending  
  7.         select e;  
  8.     //方法语法   
  9.     var q =  
  10.         db.Employees  
  11.         .OrderBy(e => e.FirstName)  
  12.         .ThenByDescending(e => e.LastName)  
  13.         .Select(e => e);  
  14.     foreach (var item in query)  
  15.     {  
  16.         Console.WriteLine(item.FirstName);  
  17.     }  
  18. }  

 

 

5. Reverse

Reverse将会把序列中的元素按照从后到前的循序反转。需要注意的是,Reverse方法的返回值是void,以下代码演示了这一点:

    1. using (NorthwindDataContext db = new NorthwindDataContext())  
    2. {  
    3.     //方法语法   
    4.     var q =  
    5.         db.Employees  
    6.         .Select(e => e.FirstName)  
    7.         .ToList();  
    8.     q.Reverse();  
    9.     foreach (var item in q)  
    10.     {  
    11.         Console.WriteLine(item);  
    12.     }  
    13.  

posted @ 2015-03-20 17:55  好好學習  阅读(886)  评论(0编辑  收藏  举报