Linq语法详细 转:https://www.cnblogs.com/knowledgesea/p/3897665.html 闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些linq新手,如果有其它疑问,可以进右上角群,进行交流探讨,谢谢。
开门见山 读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的理解,希望不要嫌我啰嗦。
1.简单的linq语法
//1var ss = from r in db.Am_recProSchemeselect r;//2var ss1 = db.Am_recProScheme;//3string sssql = "select * from Am_recProScheme";
2.带where的查询
//1var ss = from r in db.Am_recProSchemewhere r.rpId > 10select r;//2var ss1 = db.Am_recProScheme.Where(p => p.rpId > 10);//3string 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_recProSchemeselect 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);//3string 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_recProSchemewhere r.rpId > 10orderby 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.跳过前面多少条数据取余下的数据
//1var ss = (from r in db.Am_recProSchemeorderby r.rpId descendingselect r).Skip(10); //跳过前10条数据,取10条之后的所有数据 //2 var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Skip(10).ToList();//3string sssql = "select * from (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10";
7.分页数据查询
//1var ss &#61; (from r in db.Am_recProSchemewhere r.rpId > 10orderby r.rpId descendingselect r).Skip(10).Take(10); //取第11条到第20条数据 //2 Take(10): 数据从开始获取&#xff0c;获取指定数量&#xff08;10&#xff09;的连续数据var ss1 &#61; db.Am_recProScheme.OrderByDescending(p &#61;> p.rpId).Where(p &#61;> p.rpId > 10).Skip(10).Take(10).ToList();//3string sssql &#61; "select * from (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10 and rowNum<&#61;20";
8.包含&#xff0c;类似like &#39;%%&#39;
//1var ss &#61; from r in db.Am_recProSchemewhere r.SortsText.Contains("张")select r;//2var ss1 &#61; db.Am_recProScheme.Where(p &#61;> p.SortsText.Contains("张")).ToList();//3string sssql &#61; "select * from Am_recProScheme where SortsText like &#39;%张%&#39;";
9.分组group by
//1var ss &#61; from r in db.Am_recProSchemeorderby r.rpId descendinggroup r by r.recType into nselect new{n.Key, //这个Key是recTyperpId &#61; n.Sum(r &#61;> r.rpId), //组内rpId之和MaxRpId &#61; n.Max(r &#61;> r.rpId),//组内最大rpIdMinRpId &#61; n.Min(r &#61;> r.rpId), //组内最小rpId};foreach (var t in ss){Response.Write(t.Key &#43; "--" &#43; t.rpId &#43; "--" &#43; t.MaxRpId &#43; "--" &#43; t.MinRpId);}//2var ss1 &#61; from r in db.Am_recProSchemeorderby r.rpId descendinggroup r by r.recType into nselect n;foreach (var t in ss1){Response.Write(t.Key &#43; "--" &#43; t.Min(p &#61;> p.rpId));}//3var ss2 &#61; db.Am_recProScheme.GroupBy(p &#61;> p.recType);foreach (var t in ss2){Response.Write(t.Key &#43; "--" &#43; t.Min(p &#61;> p.rpId));}//4string sssql &#61; "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";
10.连接查询
//1var ss &#61; from r in db.Am_recProSchemejoin w in db.Am_Test_Result on r.rpId equals w.rsIdorderby r.rpId descendingselect r;//2var ss1 &#61; db.Am_recProScheme.Join(db.Am_Test_Result, p &#61;> p.rpId, r &#61;> r.rsId, (p, r) &#61;> p).OrderByDescending(p &#61;> p.rpId).ToList();//3string sssql &#61; "select r.* from [Am_recProScheme] as r inner join [dbo].[Am_Test_Result] as t on r.[rpId] &#61; t.[rsId] order by r.[rpId] desc";
11.sql中的In
按 Ctrl&#43;C 按 Ctrl&#43;C
近期比较忙&#xff0c;但还是想写点什么&#xff0c;就分享一些基础的知识给大家看吧&#xff0c;希望能帮助一些linq新手&#xff0c;如果有其它疑问&#xff0c;可以进右上角群&#xff0c;进行交流探讨&#xff0c;谢谢。
开门见山读这篇文章之前&#xff0c;我先说下&#xff0c;每一种搜索结果集&#xff0c;我都以三种方式变现出来&#xff0c;为啦更好的理解&#xff0c;希望不要嫌我啰嗦。
1.简单的linq语法
//1var ss &#61; from r in db.Am_recProSchemeselect r;//2var ss1 &#61; db.Am_recProScheme;//3string sssql &#61; "select * from Am_recProScheme";
2.带where的查询
//1var ss &#61; from r in db.Am_recProSchemewhere r.rpId > 10select r;//2var ss1 &#61; db.Am_recProScheme.Where(p &#61;> p.rpId > 10);//3string sssql &#61; "select * from Am_recProScheme where rpid>10";
3.简单的函数计算&#xff08;count&#xff0c;min&#xff0c;max&#xff0c;sum&#xff09;
//1获取最大的rpId//var ss &#61; (from r in db.Am_recProScheme// select r).Max(p &#61;> p.rpId);获取最小的rpId//var ss &#61; (from r in db.Am_recProScheme// select r).Min(p &#61;> p.rpId);//获取结果集的总数//var ss &#61; (from r in db.Am_recProScheme // select r).Count();//获取rpId的和var ss &#61; (from r in db.Am_recProSchemeselect r).Sum(p &#61;> p.rpId);//2//var ss1 &#61; db.Am_recProScheme.Max(p&#61;>p.rpId);//var ss1 &#61; db.Am_recProScheme.Min(p &#61;> p.rpId);//var ss1 &#61; db.Am_recProScheme.Count() ;var ss1 &#61; db.Am_recProScheme.Sum(p &#61;> p.rpId);Response.Write(ss);//3string sssql &#61; "select max(rpId) from Am_recProScheme";sssql &#61; "select min(rpId) from Am_recProScheme";sssql &#61; "select count(1) from Am_recProScheme";sssql &#61; "select sum(rpId) from Am_recProScheme";
4.排序order by desc/asc
var ss &#61; from r in db.Am_recProSchemewhere r.rpId > 10orderby r.rpId descending //倒序// orderby r.rpId ascending //正序select r;//正序var ss1 &#61; db.Am_recProScheme.OrderBy(p &#61;> p.rpId).Where(p &#61;> p.rpId > 10).ToList();//倒序var ss2 &#61; db.Am_recProScheme.OrderByDescending(p &#61;> p.rpId).Where(p &#61;> p.rpId > 10).ToList();string sssql &#61; "select * from Am_recProScheme where rpid>10 order by rpId [desc|asc]";
5.top(1)
//如果取最后一个可以按倒叙排列再取值var ss &#61; (from r in db.Am_recProScheme select r).FirstOrDefault();//&#xff08;&#xff09;linq to ef 好像不支持 Last() var ss1 &#61; db.Am_recProScheme.FirstOrDefault();//var ss1 &#61; db.Am_recProScheme.First(); string sssql &#61; "select top(1) * from Am_recProScheme";
6.跳过前面多少条数据取余下的数据
//1var ss &#61; (from r in db.Am_recProSchemeorderby r.rpId descendingselect r).Skip(10); //跳过前10条数据&#xff0c;取10条之后的所有数据 //2 var ss1 &#61; db.Am_recProScheme.OrderByDescending(p &#61;> p.rpId).Skip(10).ToList();//3string sssql &#61; "select * from (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10";
7.分页数据查询
//1var ss &#61; (from r in db.Am_recProSchemewhere r.rpId > 10orderby r.rpId descendingselect r).Skip(10).Take(10); //取第11条到第20条数据 //2 Take(10): 数据从开始获取&#xff0c;获取指定数量&#xff08;10&#xff09;的连续数据var ss1 &#61; db.Am_recProScheme.OrderByDescending(p &#61;> p.rpId).Where(p &#61;> p.rpId > 10).Skip(10).Take(10).ToList();//3string sssql &#61; "select * from (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10 and rowNum<&#61;20";
8.包含&#xff0c;类似like &#39;%%&#39;
//1var ss &#61; from r in db.Am_recProSchemewhere r.SortsText.Contains("张")select r;//2var ss1 &#61; db.Am_recProScheme.Where(p &#61;> p.SortsText.Contains("张")).ToList();//3string sssql &#61; "select * from Am_recProScheme where SortsText like &#39;%张%&#39;";
9.分组group by
//1var ss &#61; from r in db.Am_recProSchemeorderby r.rpId descendinggroup r by r.recType into nselect new{n.Key, //这个Key是recTyperpId &#61; n.Sum(r &#61;> r.rpId), //组内rpId之和MaxRpId &#61; n.Max(r &#61;> r.rpId),//组内最大rpIdMinRpId &#61; n.Min(r &#61;> r.rpId), //组内最小rpId};foreach (var t in ss){Response.Write(t.Key &#43; "--" &#43; t.rpId &#43; "--" &#43; t.MaxRpId &#43; "--" &#43; t.MinRpId);}//2var ss1 &#61; from r in db.Am_recProSchemeorderby r.rpId descendinggroup r by r.recType into nselect n;foreach (var t in ss1){Response.Write(t.Key &#43; "--" &#43; t.Min(p &#61;> p.rpId));}//3var ss2 &#61; db.Am_recProScheme.GroupBy(p &#61;> p.recType);foreach (var t in ss2){Response.Write(t.Key &#43; "--" &#43; t.Min(p &#61;> p.rpId));}//4string sssql &#61; "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";
10.连接查询
//1var ss &#61; from r in db.Am_recProSchemejoin w in db.Am_Test_Result on r.rpId equals w.rsIdorderby r.rpId descendingselect r;//2var ss1 &#61; db.Am_recProScheme.Join(db.Am_Test_Result, p &#61;> p.rpId, r &#61;> r.rsId, (p, r) &#61;> p).OrderByDescending(p &#61;> p.rpId).ToList();//3string sssql &#61; "select r.* from [Am_recProScheme] as r inner join [dbo].[Am_Test_Result] as t on r.[rpId] &#61; t.[rsId] order by r.[rpId] desc";