LINQ整理 简单的linq语法 1 2 3 4 5 6 7 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; //3 string sssql = "select * from Am_recProScheme" ; 带where的查询 1 2 3 4 5 6 7 8 var ss = from r in db.Am_r
LINQ整理
简单的linq语法
1 2 3 4 5 6 7
var ss = from r in db.Am_recProScheme select r; var ss1 = db.Am_recProScheme;string sssql = "select * from Am_recProScheme" ;
带where的查询
1 2 3 4 5 6 7 8
var ss = from r in db.Am_recProScheme where r.rpId > 10 select r; var ss1 = db.Am_recProScheme.Where(p => p.rpId > 10 );string sssql = "select * from Am_recProScheme where rpid>10" ;
简单的函数计算(count,min,max,sum)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
var ss = (from r in db.Am_recProScheme select r).Max(p => p.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(); var ss = (from r in db.Am_recProScheme select r).Sum(p => p.rpId); 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); 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" ;
排序order by desc/asc
1 2 3 4 5 6 7 8 9 10
var ss = from r in db.Am_recProScheme where r.rpId > 10 orderby r.rpId descending 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]" ;
top(1)
1 2 3 4 5 6 7
var ss = (from r in db.Am_recProScheme select r).FirstOrDefault(); var ss1 = db.Am_recProScheme.FirstOrDefault();string sssql = "select top(1) * from Am_recProScheme" ;
跳过前面多少条数据取余下的数据
1 2 3 4 5 6 7 8
var ss = (from r in db.Am_recProScheme orderby r.rpId descending select r).Skip(10 ); var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Skip(10 ).ToList();string sssql = "select * from (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10" ;
分页数据查询
1 2 3 4 5 6 7 8 9
var ss = (from r in db.Am_recProScheme where r.rpId > 10 orderby r.rpId descending select r).Skip(10 ).Take(10 ); var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > 10 ).Skip(10 ).Take(10 ).ToList();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" ;
包含,类似like ‘%%’
1 2 3 4 5 6 7 8
var ss = from r in db.Am_recProScheme where r.SortsText.Contains("张" ) select r; var ss1 = db.Am_recProScheme.Where(p => p.SortsText.Contains("张" )).ToList();string sssql = "select * from Am_recProScheme where SortsText like '%张%'" ;
分组group by
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
var ss = from r in db.Am_recProScheme orderby r.rpId descending group r by r.recType into n select new { n.Key, rpId = n.Sum(r => r.rpId), MaxRpId = n.Max(r => r.rpId), MinRpId = n.Min(r => r.rpId), }; foreach (var t in ss) { Response.Write(t.Key + "--" + t.rpId + "--" + t.MaxRpId + "--" + t.MinRpId); } 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)); } var ss2 = db.Am_recProScheme.GroupBy(p => p.recType);foreach (var t in ss2){ Response.Write(t.Key + "--" + t.Min(p => p.rpId)); } string sssql = "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType" ;
连接查询
1 2 3 4 5 6 7 8 9
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; var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList();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" ;
sql中的In
1 2 3 4 5 6 7 8 9 10
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); } string st = "select * from Am_recProScheme where rpId in(24,25,26)" ;
参考资料
原文:大专栏 LINQ整理