SQL

LINQ Lambda
 SELECT *FROM Employees

from e in Employees  select e

Employees .Select (e => e) 

SELECT e.LoginID,e.JobTitle FROM

Employees AS e

from e in Employees

select   new {e.LoginID,e.JobTitle}

Employees.Select(

e=>new{

LoginID=e.LoginID

JobTitle=e.Jobtitle }

)

 变量取别名

SELECT e.LoginID AS ID,e.JobTitle AS Job

FROM Employees AS e

from e in Employees select

new {ID=e.LoginID,Job=e.JobTitle}

Employees.Select (

e=>new{

ID=e.LoginID

Job=e.JobTitle }

)

关键字:DISTINCT 

SELECT DISTINCT e.JobTitle FROM Employees

(from  e in Employees select e.JobTitle).Distinct() 

Employees.Select(e=>e.JobTitle)

.Distinct()

关键字:WHERE

SELECT * FROM Employees AS e

WHERE e.LoginID='test'

from e in Employees

where e.LoginID='test'

select e

 Empoyees.where(e=>e.LoginID='test')  
 WHERE....AND....

SELECT * FROM Employees AS e

WHERE e.LoginID='test'

AND e.SalsriedFlag=1

from e in Employees

where e.LoginID='test' && e.SalariedFlag=1

Employee.where(

e=>(e.LoginID='test' && e.SalariedFlag=1) )

 条件运算符>=

SELECT  * FROM Employee AS e

WHERE e.VacationHours >= 2

AND e.VacationHours <= 10

from e in Employees

where e.VacationHours>=2

&& e.VacationHours<=10

Employees.where(

e=>  ( (Int32) e.VacationHours >= 2

&& (Int32)e.VacationHours <= 10 )

)

ORDER BY

SELECT  * FROM Employees AS e

ORDER BY e.NationalIDNumber

 

from e in Employees

orderby e.NationalIDNumber

select e

Employees

.orderby (e=>e.NationalIDNumber)

SELECT * FROM Employees AS e

ORDER BY e.HireDate,e.NationalIDNumber

from e in Employees

orderby e.HireDate,e.NationalIDNumber

select e

 Employees

.OrderByDescending (e=>e.HireDate)

.ThenBy (e=>e.NationalIDNumber)

关键字:LIKE 

SELECT * FROM Employees AS 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

.Where(e=>(e.JobTitle.StartsWith("Vice")

|| (e.JobTitle.Substring(0,3)=="Pro") ) )

关键字:SUM

SELECT SUM(e.VacationHours)

FROM Employees AS e

( from e in Employees

select e.VacationHours).Sum()

 Employees.Sum(e=>e.VacationHous)

关键字:COUNT 

SELECT COUNT(*) FROM Employees AS e

( from e in Employees select e).Count()

 Employees.Count();

GROUP BY

SELECT

SUM(e.VacationHours) AS TotalVacation,

e.JobTitle FROM Employees AS e

GROUP BY e.JobTitle

from e in Employees

group  e by e.JobTitle into g

select new{

JobTitle=g.Key,

Totalvacation=g.Sum(e=>e.VacationHours)

}

Employees

.GroupBy (e=>e.JobTitle)

.Select ( g=>new{

JobTitle=g.Key,

TotalVacation=g.Sum

(e => (Int32)(e.VacationHours))      }

)

关键字:HAVING

SELECT e.JobTitle,

SUM(e.VacationHours) AS TotalVacation

FROM Employees 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,

TotalVacation=g.Sum(e=>e.vacationHours)}

Employees

.GroupBy (e=>e.JobTitle)

.Where (g=>g.Count()>2)

.Select( g=>new{

JobTitle=g.JobTitle,

TotalVacation=

g.Sum(e => (Int32)(e.VacationHours)) })

多表查询

SELECT * FROM Products AS p,

ProductReviews AS pr

from p in Products

from pr in ProductReviews

select new {p,pr}

Products

.SelectManay(

p=>ProductReviews,

(p,pr)=>new{

p=p;

pr=pr })

INNER JOIN

SELECT * FROM Products AS p

INNER JOIN ProductReviews 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

.Join(

ProductReviews,p=>p.ProductID,

pr=>pr.ProductID,

(p,pr)=>new{

p=p,pr=pr}

)

JOIN.. ON...AND...

SELECT * FROM Products AS p

INNER JOIN ProductCostHistory AS pch

ON p.ProductID = pch.ProductID

AND p.SellStartDate = pch.StartDate

from p in Poducts

join pch in ProductCostHistory

on new{ p.ProductID,StartDate=p.SellStartDate}

equals new

{pch.ProductID,StartDate=p.SellStartDate}

select new {p,pch}

Products

.Join(

ProductCostHistory,

p=>new{ProductID=p.ProductID,

StartDate=p.SellStartDate},

pch =>new{ProductID=pch.ProductID,

StartDate=pch.SellStartDate},

(p,pch)=>new{p=p,pch=pch}

)

LEFT OUTER JOIN

SELECT * FROM Products AS p

LEFT OUTER JOIN ProductReviews 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

.GroupJoin (

ProductReviews,

p=>p.ProductId,

pr=>pr.ProductID,

(p,prodrev)=>new{p=p,prodrev=prodrev}

)

 关键字:UNION

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=p.ProductReviewID}

)

Products

.Select(

p=>new{ID=p.ProductID}

).Union(

ProductReviews

.Select(

pr=>new{ID=pr.ProductReviewID} )

)

SELECT TOP(10) *

FROM Products AS p

WHERE p.StandardCost<100

(from p in Products

where p.StandardCost<100

select p).Take(10)

Products

.Where(p=>p.StandardCost<100)

.Take(10)

嵌套查询 

SELECT * FROM Products AS p

WHERE p.ProductID IN(

SELECT pr.ProductID

FROM ProductReviews 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

.Where(

p=>ProductReviews

.Where(pr=>pr.Rating==5)

.Select(pr=>pr.ProductID)

.Contains(p.ProductID)

)

SELECT AVERAGE(e.Age)

FROM  Employee AS e

...MIN(xx)...

...MAX(xx)...

(from e in Employee

select e.Age).Average()

...Min()...

...Max()...

Employees.Average(e.Age)

...Min()...

...Max()...

解释:

UNION 操作符

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。

UNION 语法
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

注释:默认地,UNION 操作符选取不同的值,即UNION是去了重的。如果允许重复的值,请使用 UNION ALL。

UNION ALL 语法
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

另外,UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

UNION 指令的目的是将两个 SQL 语句的结果合并起来。从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料。union只是将两个结果联结起来一起显示,并不是联结两个表

SQL-LINQ-Lambda 语法对照的更多相关文章

  1. SQL,LINQ,Lambda语法对照图(转载)

    如果你熟悉SQL语句,当使用LINQ时,会有似曾相识的感觉.但又略有不同.下面是SQL和LINQ,Lambda语法对照图 SQL LINQ Lambda SELECT * FROM HumanReso ...

  2. SQL,Linq,Lambda之间的转换练习

    1.查询Student表中的所有记录的Sname.Ssex和Class列. SQL:select sname,ssex,class from Students linq:from s in Stude ...

  3. sql linq lambda 对比

    . 查询Student表中的所有记录的Sname.Ssex和Class列. select sname,ssex,class from student Linq: from s in Students ...

  4. SQL/LINQ/Lamda 写法[转发]

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  5. SQL Linq lamda区别

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  6. SQL/LINQ/Lamda

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  7. SQL-LINQ-Lambda语法对照,好记性不如烂笔头

    忘记的时候就翻阅翻阅吧~~ SQL LINQ Lambda SELECT *FROM HumanResources.Employee from e in Employees select e Empl ...

  8. SQL-LINQ-Lambda语法对照

    SQL LINQ Lambda SELECT *FROM HumanResources.Employee from e in Employees select e Employees .Select ...

  9. LINQ之路 4:LINQ方法语法

    书写LINQ查询时又两种语法可供选择:方法语法(Fluent Syntax)和查询语法(Query Expression). LINQ方法语法是非常灵活和重要的,我们在这里将描述使用链接查询运算符的方 ...

随机推荐

  1. Intellij Idea安装主题

    IDEA中jar包形式的主题比较常见.(顺便给大家推荐一个主题站:http://www.ideacolorthemes.org/themes/) 从主菜单中依次选择[File]>[Import ...

  2. 在Java中编写带占位符的SQL语句

    C#中SQL中带占位符的语句 假设有一张学员信息表Student,通过表中的ID来找学员,查询的SQL语句如下 string sql = string.Format("select * fr ...

  3. sketchup 导出 fbx文件 单位 错误

    最近在使用sketchup导出fbx文件到unity中使用时,发生了尺度单位上的错误.按照网上给出的标准教程,选定模型的单位为十进制-米.导出时选项选择'米',但是得到的fbx文件在unity中出现了 ...

  4. (转) Pointers

    原地址 http://www.cplusplus.com/doc/tutorial/pointers/ Pointers In earlier chapters, variables have bee ...

  5. (原)Ubuntu16中卸载并重新安装google的Protocol Buffers

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5782992.html 目前最新的是1.6.1 1. 卸载掉老版本的Protocol: sudo apt ...

  6. (原+转)VS2013:正在从以下位置加载符号

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5158020.html 这段时间启动调试时,vs2013加载时间很长很长...然后下面网址给出了解决方法 ...

  7. (原)Opencv中直方图均衡和图像动态范围拉伸的代码

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5102032.html 参考网址: http://blog.csdn.net/abcjennifer/a ...

  8. [C++程序设计]用函数指针变量调用函数

    指针变量也可以指向一个函数.一个函数在编译时被分配给一个入口地址.这个函数入口地址就称为函数的指针.可以用一个指针变量指向函数,然后通过该指针变量调用此函数 #include <iostream ...

  9. QT中的OpcDa 客户端 实现

    前段时间开发Windows下的设备端软件接触到了OPC DA,虽然现在有了更强大的OPC UA,但是为了兼容一些老的设备,不得不硬着头皮去啃这个老掉牙的已经过时了的技术.本来只是想粗略了解一下,简单写 ...

  10. python运维开发(二十三)---tornado框架

    内容目录: 路由系统 模板引擎 cookie 加密cookie 自定义api 自定义session 自定义form表单验证 异步非阻塞 web聊天室实例 路由系统 路由系统其实就是 url 和 类 的 ...