C# LINQ语法详解

C# LINQ语法详解,第1张

概述1.简单的linq语法 //1var ss = from r in db.Am_recProScheme select r;//2var ss1 = db.Am_recProScheme;//3string sssql = "select * from Am_recProScheme"; 2.带where的查询 //1var ss = from r in 1.简单的linq语法
//1var ss = from r in db.Am_recProScheme         select r;//2var ss1 = db.Am_recProScheme;//3string sssql = "select * from Am_recProScheme";
2.带where的查询
//1var ss = from r in db.Am_recProScheme         where r.rpID > 10         select 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_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();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_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.sql中的In
//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);}//sqlstring st = "select * from Am_recProScheme where rpID in(24,25,26)";
11.内连接 INNER JOIN
//linqvar 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;//Lambdavar ss1 = db.Am_recProScheme.Join(db.Am_Test_Result,p => p.rpID,r => r.rsID,(p,r) => p).OrderByDescending(p => p.rpID).ToList();//sqlstring 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";
12.左连接 left JOIN
//两个Datatable关联,查找只在第一个表中的数据static voID Main(string[] args){    Datatable dt1 = new Datatable("Ta");    Datatable dt2 = new Datatable("Tb");    dt1.Columns.Add("ID",typeof(int));    dt1.Columns.Add("name",typeof(string));    dt1.Rows.Add(1,"小明");    dt1.Rows.Add(2,"小红");    dt1.Rows.Add(3,"小黑");    dt2.Columns.Add("ID",typeof(int));    dt2.Columns.Add("name",typeof(string));    dt2.Rows.Add(1,"小黄");    dt2.Rows.Add(2,"小红");    dt2.Rows.Add(3,"小强");    //方法一:linq    //var query = from q1 in dt1.AsEnumerable()    //            join q2 in dt2.AsEnumerable()    //            on q1.FIEld<string>("name") equals q2.FIEld<string>("name") into tmp    //            from q3 in tmp.DefaultIfEmpty()    //            where q3 == null    //            select new    //            {    //                ID = q1.FIEld<int>("ID"),//                name = q1.FIEld<string>("name")    //            };    //方法二:Lambda    var query = dt1.AsEnumerable().GroupJoin(        dt2.AsEnumerable(),x => x.FIEld<string>("name"),y => y.FIEld<string>("name"),(x,y) => y.DefaultIfEmpty(). Where(w => w == null).        Select(z => new { ID = x.FIEld<int>("ID"),name = x.FIEld<string>("name") })        ).SelectMany(x => x);    foreach (var item in query)    {        Console.Writeline($"ID={item.ID}    name={item.name}");    }    Console.Read();}
实例用法:
//数据库 + 自定义名称 =new 数据库mydbDataContext con = new mydbDataContext();//模糊查询表达式中用.Containscon.car.Where(r=>r.name.Contains(TextBox1.Text.Trim())).ToList();//开头查用.StartWithcon.car.Where(r => r.name.StartsWith(TextBox1.Text)).ToList();//结尾查用.EnDWithcon.car.Where(r => r.name.EndsWith(TextBox1.Text)).ToList();//最大值con.car.Max(r => r.price * r.oil).ToString();//最小值con.car.Min(r => r.price).ToString();//求和con.car.Sum(r => r.price).ToString();//平均值con.car.Average(r => r.price).ToString();//升序:con.car.OrderBy(r => r.price).ToList();//降序:con.car.OrderByDescending(r => r.price).ToList();//上一页,下一页,组合查询:int PageCount = 5;//每页显示条数//上一页,PageCount_Label.Text为当前页码int pageNum = Convert.ToInt32(PageCount_Label.Text) - 1;Repeater1.DataSource = con.car.Skip((pageNum - 1) * PageCount).Take(PageCount);Repeater1.DataBind(); PageCount_Label.Text = pageNum.ToString();//下一页int pageNum = Convert.ToInt32(PageCount_Label.Text) + 1;Repeater1.DataSource = con.car.Skip((pageNum - 1) * PageCount).Take(PageCount);Repeater1.DataBind();PageCount_Label.Text = pageNum.ToString();//组合查询的点击事件List<car> List = con.car.ToList();if (TextBox2.Text != ""){    List<car> List1 = con.car.Where(r => r.name.Contains(TextBox2.Text)).ToList();    List = List.Intersect(List1).ToList();}if (TextBox3.Text != ""){    List<car> List1 = con.car.Where(r => r.oil == Convert.ToDecimal(TextBox3.Text)).ToList();    List = List.Intersect(List1).ToList();}if (TextBox4.Text != ""){    List<car> List1 = con.car.Where(r => r.powers == Convert.ToInt32(TextBox4.Text)).ToList();    List = List.Intersect(List1).ToList();}Repeater1.DataSource = List;Repeater1.DataBind();
总结

以上是内存溢出为你收集整理的C# LINQ语法详解全部内容,希望文章能够帮你解决C# LINQ语法详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1214885.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存