Linq语法详细(转)
原文地址:http://www.cnblogs.com/knowledgesea/p/3897665.html
开门见山
读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的理解,希望不要嫌我啰嗦。
1.简单的linq语法
//
var ss = from r in db.Am_recProScheme
select r;
//
var ss1 = db.Am_recProScheme;
//
string sssql = "select * from Am_recProScheme";
2.带where的查询
//
var ss = from r in db.Am_recProScheme
where r.rpId >
select r;
//
var ss1 = db.Am_recProScheme.Where(p => p.rpId > );
//
string sssql = "select * from Am_recProScheme where rpid>10";
3.简单的函数计算(count,min,max,sum)
//
////获取最大的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); //
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 >
orderby r.rpId descending //倒序
// orderby r.rpId ascending //正序
select r; //正序
var ss1 = db.Am_recProScheme.OrderBy(p => p.rpId).Where(p => p.rpId > ).ToList();
//倒序
var ss2 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > ).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.跳过前面多少条数据取余下的数据
//
var ss = (from r in db.Am_recProScheme
orderby r.rpId descending
select r).Skip(); //跳过前10条数据,取10条之后的所有数据
//2
var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Skip().ToList();
//
string sssql = "select * from (select ROW_NUMBER()over(order by rpId desc) as rowNum, * from [Am_recProScheme]) as t where rowNum>10";
7.分页数据查询
//
var ss = (from r in db.Am_recProScheme
where r.rpId >
orderby r.rpId descending
select r).Skip().Take(); //取第11条到第20条数据 //2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据
var ss1 = db.Am_recProScheme.OrderByDescending(p => p.rpId).Where(p => p.rpId > ).Skip().Take().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";
8.包含,类似like '%%'
//
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 '%张%'";
9.分组group by
//
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);
}
//
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";
10.连接查询
//
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";
11.sql中的In
//
var ss = from p in db.Am_recProScheme
where (new int?[] { , , }).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语法详细(转)的更多相关文章
- Linq语法详细
闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些linq新手,如果有其它疑问,可以进右上角群,进行交流探讨,谢谢. 开门见山 读这篇文章之前,我先说下,每一种搜索 ...
- Linq常用语法详细
1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; // ...
- Lamda和Linq语法对比详细
本人转载:http://www.cnblogs.com/knowledgesea/p/3897665.html 闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些l ...
- Linq To Sql中实现Left Join与Inner Join使用Linq语法与lambda表达式
当前有两个表,sgroup与sgroupuser,两者通过gKey关联,而sgroup表记录的是组,而sgroupuser记录是组中的用户,因此在sgroupuser中不一定有数据.需要使用Left ...
- Linq语法
希望能帮助一些linq新手. 开门见山 读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的理解,希望不要嫌我啰嗦. 1.简单的linq语法 //1 var ss = fro ...
- Linq 语法举例
1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; // ...
- LinQ 语法基础
LINQ (Language-Integrated Query,语言集成查询). LINQ to Objects.LINQ to SQL.LINQ to DataSet和LINQ to XML,它们分 ...
- Linq语法常见普通语法
闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些linq新手,如果有其它疑问,可以进右上角群,进行交流探讨,谢谢. 开门见山 读这篇文章之前,我先说下,每一种搜索 ...
- C# LINQ语法详解
1.简单的linq语法 var ss = from r in db.Am_recProScheme select r; var ss1 = db.Am_recProScheme; string sss ...
随机推荐
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
Android中View的绘制过程 onMeasure方法简述 附有自定义View例子 Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android fr ...
- umeng 渠道统计 android
1.配置AndroidManifest.xml,添加权限 <uses-permission android:name="android.permission.ACCESS_NETWOR ...
- JSON 转javabean 利器
别再对着json来手写javabean啦.这个工作完全不要脑子,而且耗时. 这里给大家提供三种方式: android studio版: 万能的插件:GsonFormat 如何安装? Preferenc ...
- 【代码笔记】iOS-提醒时间的选择
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- 【读书笔记】iOS网络-错误处理的经验法则
一,在接口契约中处理错误. 二,错误状态可能不正确. 设备模糊地确认操作是崇拜失败的.比如,移动应用发出HTTP请求以在两个账户间转账.请求被银行系统接收并正确地处理:然而,由于网络失败应答却丢失了, ...
- 同步时间linux
针对对时间要求精确度高的服务器 1.安装时间服务器yum install ntp 2.同步时间ntpdate time.nist.gov 3.设置计划任务每隔10分钟同步一次 */10 * * * * ...
- 检测PC端和移动端的方法之一
window.mobileCheck = function() { var check = false; (function(a){if(/(android|bb\d+|meego).+mobile| ...
- spring定时器(二)
此定时器可重置定时时间. 1. spring的定时器配置文件application.xml: <?xml version="1.0" encoding="UTF-8 ...
- php遍历循环数组实现方法
简单利用foreach for list each while来遍历数组,包括普通的一维数组与二维数组遍历方法,下面详细的介绍了每个函数的使用方法. $foreach = array(1,2,3); ...
- PDO链接mysql学习笔记
<?php //PDO链接mysql//dsn三种写法: //dsn01 $dsn = 'mysql:host=localhost;dbname=mysql'; //$dsn = 'mysql: ...