linq查询语法和方法-簡單用法
來自:http://www.cnblogs.com/knowledgesea/p/3897665.html
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)";
1. Select
Select操作符对单个序列或集合中的值进行投影。下面的示例中使用select从序列中返回Employee表的所有列:
- using (NorthwindDataContext db=new NorthwindDataContext())
- {
- //linq查询语法(基本语法)
- var query =
- from e in db.Employees
- where e.FirstName.StartsWith("M")
- select e;
- //linq方法语法(基本方法)
- var q =
- db.Employees
- .Where(e => e.FirstName.StartsWith("M"))
- .Select(e => e);
- foreach (var item in query)
- {
- Console.WriteLine(item.FirstName);
- }
- }
当然,你也可以返回单个列,例如:
- var query =
- from e in db.Employees
- where e.FirstName.StartsWith("M")
- select e.FirstName;
你也可以返回序列中的某几列,例如:
- var query =
- from e in db.Employees
- where e.FirstName.StartsWith("M")
- select new
- {
- e.FirstName,
- e.LastName,
- e.Title
- };
2. SelectMany
SelectMany操作符提供了将多个from子句组合起来的功能,它将每个对象的结果合并成单个序列。下面是一个示例:
- using (NorthwindDataContext db=new NorthwindDataContext())
- {
- //查询语法
- var query =
- from e in db.Employees
- from o in e.Orders
- select o;
- //方法语法
- var q =
- db.Employees
- .SelectMany(e => e.Orders);
- foreach (var item in query)
- {
- Console.WriteLine(item.Freight);
- }
- }
二、限制操作符
Where是限制操作符,它将过滤标准应用在序列上,按照提供的逻辑对序列中的数据进行过滤。
Where操作符不启动查询的执行。当开始对序列进行遍历时查询才开始执行,此时过滤条件将被应用到查询中。Where操作符的使用方法已经在第一节中出现过,这里不再冗述。
三、排序操作符
排序操作符,包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序。
1. OrderBy
OrderBy操作符将序列中的元素按照升序排列。下面的示例演示了这一点:
- using (NorthwindDataContext db = new NorthwindDataContext())
- {
- //查询语法
- var query =
- from e in db.Employees
- orderby e.FirstName
- select e;
- //方法语法
- var q =
- db.Employees
- .OrderBy(e => e.FirstName)
- .Select(e => e);
- foreach (var item in q)
- {
- Console.WriteLine(item.FirstName);
- }
- }
这里可以使用OrderBy的重载方法OrderBy(Func,IComparer)来指定序列的排序方式。
2. OrderByDescending
OrderByDescending操作符将序列中的元素按照降序排列。用法与OrderBy相同,这里不再演示。
3. ThenBy
ThenBy操作符实现按照次关键字对序列进行升序排列。此操作符的查询语法与方法语法略有不同,以下代码演示了这一点:
- using (NorthwindDataContext db = new NorthwindDataContext())
- {
- //查询语法
- var query =
- from e in db.Employees
- orderby e.FirstName,e.LastName
- select e;
- //方法语法
- var q =
- db.Employees
- .OrderBy(e => e.FirstName)
- .ThenBy(e => e.LastName)
- .Select(e => e);
- foreach (var item in query)
- {
- Console.WriteLine(item.FirstName);
- }
- }
4. ThenByDescending
ThenByDescending操作符实现按照次关键字对序列进行降序排列。此操作符的查询语法与方法语法略有不同,以下代码演示了这一点:
- using (NorthwindDataContext db = new NorthwindDataContext())
- {
- //查询语法
- var query =
- from e in db.Employees
- orderby e.FirstName,e.LastName descending
- select e;
- //方法语法
- var q =
- db.Employees
- .OrderBy(e => e.FirstName)
- .ThenByDescending(e => e.LastName)
- .Select(e => e);
- foreach (var item in query)
- {
- Console.WriteLine(item.FirstName);
- }
- }
5. Reverse
Reverse将会把序列中的元素按照从后到前的循序反转。需要注意的是,Reverse方法的返回值是void,以下代码演示了这一点:
- using (NorthwindDataContext db = new NorthwindDataContext())
- {
- //方法语法
- var q =
- db.Employees
- .Select(e => e.FirstName)
- .ToList();
- q.Reverse();
- foreach (var item in q)
- {
- Console.WriteLine(item);
- }
- }
linq查询语法和方法-簡單用法的更多相关文章
- .NET LINQ查询语法与方法语法
LINQ 查询语法与方法语法 通过使用 C# 3.0 中引入的声明性查询语法,介绍性 LINQ 文档中的多数查询都被编写为查询表达式. 但是,.NET 公共语言运行时 (CLR) 本身并不具 ...
- C#3.0新增功能09 LINQ 基础07 LINQ 中的查询语法和方法语法
连载目录 [已更新最新开发文章,点击查看详细] 介绍性的语言集成查询 (LINQ) 文档中的大多数查询是使用 LINQ 声明性查询语法编写的.但是在编译代码时,查询语法必须转换为针对 .NET ...
- 二:MVC之LINQ查询语法
LINQ(Language Integrated Query)语言集成查询是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以操作内存数据的方式,查询数 ...
- Linq查询语法(2)
转:http://www.cnblogs.com/knowledgesea/p/3897665.html 1.简单linq查询 var ss = from r in db.Am_recProSchem ...
- Linq查询语法(1)
转:http://www.cnblogs.com/ahao214/archive/2013/01/22/2871044.html LINQ的基本格式如下所示:var <变量> = from ...
- 让LINQ中的查询语法使用自定义的查询方法
使用LINQ时有两种查询语法:查询语法和方法语法 查询语法:一种类似 SQL 语法的查询方式 方法语法:通过扩展方法和Lambda表达式来创建查询 例如: List<, , , }; //查询语 ...
- C#高级知识点概要(3) - 特性、自动属性、对象集合初始化器、扩展方法、Lambda表达式和Linq查询
1.特性(Attributes) 特性(Attributes),MSDN的定义是:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法 ...
- Linq 查询 与方法调用
通常,使用linq查询时需要一个实现IQueryable<T> 的查询对象 public class DataA<T> : IQueryable<T> {....} ...
- LINQ之查询语法
新开一节LINQ的入门讲解. LINQ(Language Integrated Query)语言集成查询,是C#语言的扩展,它的主要功能是从数据集中查询数据,就像通过sql语句从数据库查询数据一样(本 ...
随机推荐
- C#制作手机网站
<meta name="viewport" content="width=device-width, initial-scale=1.0"> //在 ...
- Java集成groovy之GroovyShell、GroovyScriptEngine、GroovyClassLoader
GroovyClassLoader 用 Groovy 的 GroovyClassLoader ,动态地加载一个脚本并执行它的行为.GroovyClassLoader是一个定制的类装载器,负责解释加载J ...
- nginx 安装配置和常用命令
基本环境:CentOS 1. 下载 nginx [root@localhost ~]# cd Downloads/ [root@localhost Downloads]# wget http://ng ...
- Redis-概述
Redis支持的类型: String,List,Map,Set,Sorted set Redis的持久化: 1.Redis DataBase (RDB): RDB是在某个时间点将数据写入一个临时文件, ...
- mybatis oracle 顺序模糊匹配
前言:有时需要顺序模糊匹配字段. 用一半的 % 就好: t.item like #{item}||'%'
- mySql连接报错
问题: mySql链接报错如下: 解决: 设定时区 在链接URL增加?serverTimezone=UTC,变为jdbc:mysql://localhost:3306/TEST?serverTimez ...
- javascript中函数的写法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 浅谈ul布局以及table布局
我个人对于某些言论说要注重html语义化在布局中的应用,我反而不怎么感冒,试试兼容IE7&&项目期相对较赶的情况下,我还是推荐快速开发为主,兼容性强为主. 如果布局中需要用户边框,推荐 ...
- 公共cdn的js和css库
使用cdn的js和css公用库 为什么要使用cdn,用cdn资源有什么好处了,可以看看yahoo性能建议,中间有一条的http://developer.yahoo.com/performance/ru ...
- jQuery中判断input的checked属性
<input type="checkbox" id="ipt1" checked> <input type="checkbox&qu ...