SQL、Linq、lamda表达式 同一功能不同写法
一、SQL、Linq、lamda表达式 同一功能不同写法
|
SQL |
LINQ |
Lambda |
|---|---|---|
|
SELECT * FROM HumanResources.Employee |
from e in Employees select e |
Employees |
|
SELECT e.LoginID, e.JobTitle FROM HumanResources.Employee AS e |
from e in Employees select new {e.LoginID, e.JobTitle} |
Employees |
|
SELECT e.LoginID AS ID, e.JobTitle AS Title FROM HumanResources.Employee AS e |
from e in Employees select new {ID = e.LoginID, Title = e.JobTitle} |
Employees |
|
SELECT DISTINCT e.JobTitle FROM HumanResources.Employee AS e |
(from e in Employees select e.JobTitle).Distinct() |
Employees |
|
SELECT e.* FROM HumanResources.Employee AS e WHERE e.LoginID = 'test' |
from e in Employees where e.LoginID == "test" select e |
Employees |
|
SELECT e.* FROM HumanResources.Employee AS e WHERE e.LoginID = 'test' AND e.SalariedFlag = 1 |
from e in Employees where e.LoginID == "test" && e.SalariedFlag select e |
Employees |
|
SELECT e.* WHERE e.VacationHours >= 2 AND e.VacationHours <= 10 |
from e in Employees where e.VacationHours >= 2 && e.VacationHours <= 10 select e |
Employees |
|
SELECT e.* FROM HumanResources.Employee AS e |
from e in Employees orderby e.NationalIDNumber select e |
Employees |
|
SELECT e.* FROM HumanResources.Employee AS e ORDER BY e.HireDate DESC, e.NationalIDNumber |
from e in Employees orderby e.HireDate descending, e.NationalIDNumber select e |
Employees |
|
SELECT e.* WHERE e.JobTitle LIKE 'Vice%' OR SUBSTRING(e.JobTitle, 0, 3) = 'Pro' |
from e in Employees where e.JobTitle.StartsWith("Vice") || e.JobTitle.Substring(0, 3) == "Pro" select e |
Employees |
|
SELECT SUM(e.VacationHours) FROM HumanResources.Employee AS e |
Employees.Sum(e => e.VacationHours); |
|
|
SELECT COUNT(*) FROM HumanResources.Employee AS e |
Employees.Count(); |
|
|
SELECT SUM(e.VacationHours) AS TotalVacations, e.JobTitle FROM HumanResources.Employee AS e GROUP BY e.JobTitle |
from e in Employees group e by e.JobTitle into g select new {JobTitle = g.Key, TotalVacations = g.Sum(e => e.VacationHours)} |
Employees |
|
SELECT e.JobTitle, SUM(e.VacationHours) AS TotalVacations FROM HumanResources.Employee AS e GROUP BY e.JobTitle HAVING e.COUNT(*) > 2 |
from e in Employees group e by e.JobTitle into g where g.Count() > 2 select new {JobTitle = g.Key, TotalVacations = g.Sum(e => e.VacationHours)} |
Employees |
|
SELECT * FROM Production.Product AS p, Production.ProductReview AS pr |
from p in Products from pr in ProductReviews select new {p, pr} |
Products |
|
SELECT * FROM Production.Product AS p INNER JOIN Production.ProductReview AS pr ON p.ProductID = pr.ProductID |
from p in Products join pr in ProductReviews on p.ProductID equals pr.ProductID select new {p, pr} |
Products |
|
SELECT * FROM Production.Product AS p INNER JOIN Production.ProductCostHistory AS pch ON p.ProductID = pch.ProductID AND p.SellStartDate = pch.StartDate |
from p in Products join pch in ProductCostHistories on new {p.ProductID, StartDate = p.SellStartDate} equals new {pch.ProductID, StartDate = pch.StartDate} select new {p, pch} |
Products |
|
SELECT * FROM Production.Product AS p LEFT OUTER JOIN Production.ProductReview AS pr ON p.ProductID = pr.ProductID |
from p in Products join pr in ProductReviews on p.ProductID equals pr.ProductID into prodrev select new {p, prodrev} |
Products |
|
SELECT p.ProductID AS ID FROM Production.Product AS p UNION SELECT pr.ProductReviewID FROM Production.ProductReview AS pr |
(from p in Products select new {ID = p.ProductID}).Union( from pr in ProductReviews select new {ID = pr.ProductReviewID}) |
Products |
|
SELECT TOP (10) * FROM Production.Product AS p WHERE p.StandardCost < 100 |
(from p in Products where p.StandardCost < 100 select p).Take(10) |
Products |
|
SELECT * FROM [Production].[Product] AS p WHERE p.ProductID IN( SELECT pr.ProductID FROM [Production].[ProductReview] AS [pr] WHERE pr.[Rating] = 5 ) |
from p in Products where (from pr in ProductReviews where pr.Rating == 5 select pr.ProductID).Contains(p.ProductID) select p |
Products |
二、同一功能的几种不同写法的实例
1、简单的函数计算(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";
2、排序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]";

3、top

//1

//如果取最后一个可以按倒叙排列再取值

var ss = (from r in db.Am_recProScheme select r).FirstOrDefault();


//2

string sssql = "select top(1) * from Am_recProScheme";

4、跳过前面多少条数据取余下的数据

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

5、分页数据查询

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

6、包含,类似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 '%张%'";

7、分组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";

8、连接查询

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

9、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)";

SQL、Linq、lamda表达式 同一功能不同写法的更多相关文章
- SQL/LINQ/Lamda 写法[转发]
SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees .Sele ...
- lamda表达式的两种写法
public class Test{ public synchronized void test1(){ System.out.println("test1 start........... ...
- SQL Linq lamda区别
SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees .Sele ...
- SQL/LINQ/Lamda
SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees .Sele ...
- Linq lamda表达式Single和First方法
让我们来看看如何对一个整数数组使用 Single 操作符.这个整数数组的每个元素代表 2 的 1 到 10 次方.先创建此数组,然后使用 Single 操作符来检索满足 Linq Lambda表达 ...
- C#DataTable 使用GroupBy方法的lamda 表达式和Linq语句写法
https://www.cnblogs.com/johnblogs/p/6006867.html DataTable ds = new DataTable(); //1.lamda 表达式写法(推荐) ...
- C# Linq及Lamda表达式实战应用之 GroupBy 分组统计
在项目中做统计图表的时候,需要对查询出来的列表数据进行分组统计,首先想到的是避免频繁去操作数据库可以使用 Linq eg: //例如对列表中的Cu元素进行按年GroupBy分组统计 //包含年份,平均 ...
- Linq和Lamda表达式的简单处理方式
一 什么是LINQ? LINQ即Language Integrated Query(语言集成查询),LINQ是集成到C#和Visual Basic.NET这些语言中用于提供查询数据能力的一个新特性. ...
- c# Linq及Lamda表达式应用经验之 GroupBy 分组
示例1: GroupBy 分组在List<>泛型中的应用 原表: 按姓名Nam 分组后结果: 对DATATABLE 进行LAMDA查询时必须在项目的引用中添加 System.Data.Da ...
随机推荐
- xcode8让真机测试支持ios8.0以下版本
xcode8支持ios8以下真机测试方法: 1.应用程序-xcode 显示包内容-Contents-Developer-Platforms-iPhoneOS.platform-DeviceSuppor ...
- SOA 面向服务的体系结构
SOA:面向服务的体系结构(service-oriented architecture) 是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来. 接口是采 ...
- java基础-servlet-1
servlet,是运行于服务器端的小程序.它既可以运行在http服务器端,也可以运行在mail等其他服务器端.我是这样理解,tomcat把接收到的客户端请求,转发给servlet,它负责业务处理,并且 ...
- Hibernate5.2之一对一主键关联(四)
Hibernate5.2之一对一主键关联(四) 一.简介 一对一关联关系分为两种:a.主键关联:b. ...
- Linux添加主机路由
Linux添加主机路由:参考:http://zhidao.baidu.com/link?url=50qQ232ZiJqq1iftHvBdPzZh-JlQGi1DZ8wvF-j9dvQYsKiNgc-6 ...
- 利用nodejs搭建服务器,测试AJAX
最近学习了AJAX一直没有进行过测试,前今天了解了Noejs搭建本地服务器下就尝试了一下.通过AJAX请求的方式获取HTTP服务器返回数据的代码 首先创建一个serve.js的文件.并写入以下代码. ...
- SQL Server 维护计划实现数据库备份(Step by Step)(转)
SQL Server 维护计划实现数据库备份(Step by Step) 一.前言 SQL Server 备份和还原全攻略,里面包括了通过SSMS操作还原各种备份文件的图形指导,SQL Server ...
- Python基础语法,基本数据类型及相关操作
---恢复内容开始--- python文件 文件开头要有 #!/usr/bin/ python --在linux中是告诉系统phthon的路径是在/usr/bin/ python目录下 ...
- C#中属性与字段的用法
//People.cs public class People { //字段 private string _name; //属性 作用:保护字段,对字段的取值和赋值进行限定,限制非法字段的摄入 pu ...
- 针对WebService服务,客户端调用时报序列化的最大项数maxItemsInObjectGraph超过65536问题
今天在使用webservice服务时候,报异常"The InnerException message was 'Maximum number of items that can be ser ...