Linq详细语法

2018-06-22 07:38:58来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

闲言碎语

近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些linq新手,如果有其它疑问,可以加我QQ,进行交流探讨,谢谢。

开门见山

读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的理解,希望不要嫌我啰嗦。

 

1.简单的linq语法

1 //1
2 var ss = from r in db.Am_recProScheme
3 select r;
4 //2
5 var ss1 = db.Am_recProScheme;
6 //3
7 string sssql = "select * from Am_recProScheme";

2.带where的查询

1 //1
2             var ss = from r in db.Am_recProScheme
3                      where r.rpId > 10
4                      select r;
5             //2
6             var ss1 = db.Am_recProScheme.Where(p => p.rpId > 10);
7             //3
8             string sssql = "select * from Am_recProScheme where rpid>10";

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

 1 //1
 2             ////获取最大的rpId
 3             //var ss = (from r in db.Am_recProScheme
 4             //          select r).Max(p => p.rpId);
 5             ////获取最小的rpId
 6             //var ss = (from r in db.Am_recProScheme
 7             //          select r).Min(p => p.rpId);
 8             //获取结果集的总数
 9             //var ss = (from r in db.Am_recProScheme                  
10             //         select r).Count();
11             //获取rpId的和
12             var ss = (from r in db.Am_recProScheme
13                       select r).Sum(p => p.rpId);
14 
15 
16             //2
17             //var ss1 = db.Am_recProScheme.Max(p=>p.rpId);
18             //var ss1 = db.Am_recProScheme.Min(p => p.rpId);
19             //var ss1 = db.Am_recProScheme.Count() ;
20             var ss1 = db.Am_recProScheme.Sum(p => p.rpId);
21             Response.Write(ss);
22 
23             //3
24             string sssql = "select max(rpId) from Am_recProScheme";
25                    sssql = "select min(rpId) from Am_recProScheme";
26                    sssql = "select count(1) from Am_recProScheme";
27                    sssql = "select sum(rpId) from Am_recProScheme";

 4.排序order by desc/asc

 1 var ss = from r in db.Am_recProScheme
 2                      where r.rpId > 10
 3                      orderby r.rpId descending  //倒序
 4                      //  orderby r.rpId ascending   //正序
 5                      select r;
 6 
 7             //正序
 8             var ss1 = db.Am_recProScheme.OrderBy(p => p.rpId).Where(p => p.rpId > 10).ToList();
 9             //倒序
10             var ss2 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).ToList();
11 
12             string sssql = "select * from Am_recProScheme where rpid>10 order by rpId [desc|asc]";

 5.top(1)

1 //如果取最后一个可以按倒叙排列再取值
2             var ss = (from r in db.Am_recProScheme                     
3                       select r).FirstOrDefault();
4 
5             //()linq to ef 好像不支持 Last() 
6             var ss1 = db.Am_recProScheme.FirstOrDefault();
7             //var ss1 = db.Am_recProScheme.First();          
8 
9             string sssql = "select top(1) * from Am_recProScheme";

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

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

 7.分页数据查询

 1 //1
 2             var ss = (from r in db.Am_recProScheme
 3                       where r.rpId > 10
 4                       orderby r.rpId descending
 5                       select r).Skip(10).Take(10); //取第11条到第20条数据                   
 6 
 7             //2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据
 8             var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10).Skip(10).Take(10).ToList();
 9             //3
10             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  //1
2             var ss = from r in db.Am_recProScheme
3                      where r.SortsText.Contains("")
4                      select r;
5             //2
6             var ss1 = db.Am_recProScheme.Where(p => p.SortsText.Contains("")).ToList();
7             //3
8             string sssql = "select * from Am_recProScheme where SortsText like '%张%'";

 9.分组group by

 1  //1
 2             var ss = from r in db.Am_recProScheme
 3                      orderby r.rpId descending
 4                      group r by r.recType into n
 5                      select new
 6                      {
 7                          n.Key,  //这个Key是recType
 8                          rpId = n.Sum(r => r.rpId), //组内rpId之和
 9                          MaxRpId = n.Max(r => r.rpId),//组内最大rpId
10                          MinRpId = n.Min(r => r.rpId), //组内最小rpId
11                      };
12             foreach (var t in ss)
13             {
14                 Response.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId);
15             }
16             //2
17             var ss1 = from r in db.Am_recProScheme
18                      orderby r.rpId descending
19                      group r by r.recType into n
20                      select n;
21             foreach (var t in ss1)
22             {
23                 Response.Write(t.Key + "--" + t.Min(p => p.rpId));
24             }
25             //3
26             var ss2 = db.Am_recProScheme.GroupBy(p => p.recType);
27             foreach (var t in ss2)
28             {
29                 Response.Write(t.Key + "--" + t.Min(p => p.rpId));
30             }
31             //4
32             string sssql = "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";

10.连接查询

1 //1
2             var ss = from r in db.Am_recProScheme
3                      join w in db.Am_Test_Result on r.rpId equals w.rsId
4                      orderby r.rpId descending
5                      select r;
6             //2
7             var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList();
8             //3
9             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 //1
 2             var ss = from p in db.Am_recProScheme
 3                               where (new int?[] { 24, 25,26 }).Contains(p.rpId)
 4                               select p;
 5             foreach (var p in ss)
 6             {
 7                 Response.Write(p.Sorts);
 8             }
 9             //2
10             string st = "select * from Am_recProScheme where rpId in(24,25,26)";

 

 

 

 

如果你是linq新手,建议这十个常用的查询标记学习,再加上视图,基本上就能解决大部分的查询问题啦。

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Orchard详解--第二篇 启动

下一篇:MVC页面打印成pdf插件Rotativa