C# LINQ语法详解

Posted zhaoshujie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# LINQ语法详解相关的知识,希望对你有一定的参考价值。

1.简单的linq语法

//1
var ss = from r in db.Am_recProScheme
         select r;
//2
var ss1 = db.Am_recProScheme;
//3
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);

//3
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";

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.跳过前面多少条数据取余下的数据

//1
var 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();
//3
string sssql = "select * from  (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10";

7.分页数据查询

//1
var 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();
//3
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
var ss = from r in db.Am_recProScheme
         where r.SortsText.Contains("")
         select r;
//2
var ss1 = db.Am_recProScheme.Where(p => p.SortsText.Contains("")).ToList();
//3
string sssql = "select * from Am_recProScheme where SortsText like ‘%张%‘";

9.分组group by

//1
var 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);
}
//2
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));
}
//3
var ss2 = db.Am_recProScheme.GroupBy(p => p.recType);
foreach (var t in ss2)
{
    Response.Write(t.Key + "--" + t.Min(p => p.rpId));
}
//4
string sssql = "select recType,min(rpId),max(rpId),sum(rpId) from Am_recProScheme group by recType";

10.连接查询

//1
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;
//2
var ss1 = db.Am_recProScheme.Join(db.Am_Test_Result, p => p.rpId, r => r.rsId, (p, r) => p).OrderByDescending(p => p.rpId).ToList();
//3
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
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);
}
//2
string st = "select * from Am_recProScheme where rpId in(24,25,26)";

 

实例用法:

//数据库 + 自定义名称 =new 数据库
mydbDataContext con = new mydbDataContext();
//模糊查询表达式中用.Contains
con.car.Where(r=>r.name.Contains(TextBox1.Text.Trim())).ToList();
//开头查用.StartWith
con.car.Where(r => r.name.StartsWith(TextBox1.Text)).ToList();
//结尾查用.EndWith
con.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

错误 3002:映射片段中的问题 | c# linq 到实体

在 C# 中使用 LINQ 方法语法 (LINQ to SQL) 时发生异常

C#中的Linq to Xml详解

C# 之 LINQ的查询语法

linq简介