初探Linq表达式和lambda表达式

时间:2022-01-05 18:44:31

【Linq表达式】

         LINQ,语言集成查询(LanguageIntegrated Query)是一组用于c#和Visual Basic语言的扩展。它允许编写C#或者VisualBasic代码以查询数据库相同的方式操作内存数据。

        博主认为,Linq表达式在用法方面就是SQL语句的变形,只要将SQL语句按照执行顺序来重新编写就可以得到Linq表达式。

【Lambda表达式】

        Lambda 用在基于方法的 LINQ查询中,对网上查询到的关于Lambda表达式的资料不甚了解。Lambda表达式的使用在C#中,Lambda 表达式都使用 Lambda 运算符 =>,该运算符读为“goes to”。

        博主认为:Lambda用于Linq查询中,Lambda表达式就是用Lambda语法将Linq表达式表示出来。

       下面用几个语句对Linq表达式和Lambda表达式进行区别,为了便于理解,每个例子都以SQL语句、Linq表达式、Lambda表达式三种形式呈现。

1.简单的查询

//1——Linq表达式
var ss = from r in db.Am_recProScheme
select r;
//2——Lambda表达式
var ss1 = db.Am_recProScheme;
//3——SQL语句
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);//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_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();
<span style="font-family: Calibri; font-size: 18.6667px;">string sssql = "select * from Am_recProScheme where rpid>10 order by rpId [desc|asc]";</span>
5.top(1)

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

//2 linq to ef 好像不支持 Last()
var ss1 = db.Am_recProScheme.FirstOrDefault();
//var ss1 = db.Am_recProScheme.First();
//3
string sssql = "select top(1) * from Am_recProScheme";
6.跳过前面多少条数据取余下的数据
//1var 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();//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 = (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();//3string 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 '%%'
//1var ss = from r in db.Am_recProScheme         where r.SortsText.Contains("张")         select r;//2var ss1 = db.Am_recProScheme.Where(p => p.SortsText.Contains("张")).ToList();//3string sssql = "select * from Am_recProScheme where SortsText like '%张%'";
9.分组group by
//1var 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);}//2var 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));}//3var ss2 = db.Am_recProScheme.GroupBy(p => p.recType);foreach (var t in ss2){        Response.Write(t.Key + "--" + t.Min(p => p.rpId));}//4string sssql = "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";
10.连接查询
//1var 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;//2var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList();//3string 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——Linqvar 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——SQLstring st = "select * from Am_recProScheme where rpId in(24,25,26)";