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 ...
随机推荐
- Struts1.x有两个execute方法,不要重写错哦HttpServletRequest才是对的(转)
Struts1.x 的 Action 有两个 execute 哦,小心搞错! by agate - Published: 2008-05-01 [9:42 下午] - Category: 程序编码 不 ...
- 04 Linux字符设备驱动
一.结构体 1. cdev 结构体 struct cdev { struct kobject kobj; /* 内嵌的 kobject 对象 */ struct module *owner; /*所属 ...
- JavaScript的学习4
一.Array对象 ArrayObject.reverse() 将数组进行翻转 ArrayObject.shift() 删除数组中的第一个元素并将其返回 ArrayObject.unshi ...
- C++ 队列的实现
/************************************************************************/ /* 实现一个通用同步队列 使用链表实现队列 (先 ...
- [转]linux sort,uniq,cut,wc命令详解
sort sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行排序. sort语法 ...
- How to Take Control of Your Line Height in Outlook.com
Reference to: http://www.emailonacid.com/blog/details/C13/line_height_and_outlook.com
- python 核心编程课后练习(chapter 2)
2-4 #2-4(a) print "enter a string" inputstring = raw_input() print"the string is: &qu ...
- 3、NAT
<计算机网络(第5版)>P173 P182
- DMA控制
1.S3C6410中DMA操作步骤 S3C6410中DMA操作步骤:1.决定使用安全DMAC(SDMAC)还是通用DMAC(DMAC):2.开始相应DMAC的系统时钟,并关闭另外一组的时钟(系统默认开 ...
- NOIP模拟赛-护花
[题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽 ...