Sql-Linq-Lambda对照表

本文深入探讨了SQL与LINQ查询之间的转换,从基本选择、投影到复杂的联接、分组和聚合操作。通过具体的查询示例,展示了如何在LINQ中使用Lambda表达式实现与SQL等效的功能,包括筛选、排序、分组统计和联合查询。此外,还涵盖了在处理集合时如何利用LINQ的灵活性和强类型检查提高代码的可读性和安全性。
摘要由CSDN通过智能技术生成
 Sql Linq Lambda

SELECT *

FROM Employee

from e in Employee

select e

Employee

.Select(e => e)

SELECT ID, Title

FROM Employee

from e in Employee

select new {ID = e.ID, Title = e.Title}

Employee

.Select(e => new {ID = e.ID, Title = e.Title})

SELECT DISTINCT Title

FROM Employee

(from e in Employee

select e.Title).Distinct()

Employee

.Select(e => e.Title).Distinct ()

SELECT e.*

FROM Employee AS e

WHERE e.ID = 1

from e in Employee

where e.ID == 1

select e

Employee

.Where(e => (e.ID == 1))

SELECT e.*

FROM Employee AS e

WHERE e.ID = 1 AND(OR) e.Flag = 1

from e in Employee

where e.ID == 1 &&(||) e.Flag

select e

Employee

.Where(e => ((e.ID == 1) &&(||) e.Flag))

SELECT e.*

FROM Employee AS e

WHERE e.ID >= 1 AND(OR)  e.ID <= 3

from e in Employee

where e.ID >= 1 &&(||) e.ID <= 3

select e

Employee

.Where(e => ((e.ID >= 1) &&(||) (e.ID <= 3)))

SELECT e.*

FROM Employee AS e

ORDER BY e.ID

from e in Employee

orderby e.ID

select e

Employee

.OrderBy(e => e.ID)

SELECT e.*

FROM Employee AS e

ORDER BY e.ID DESC, e.Title

from e in Employee

orderby e.ID descending, e.Title

select e

Employee

.OrderByDescending(e => e.ID)

.ThenBy(e => e.Title)

SELECT SUM(e.Times)

FROM Employee

Employee

.Sum(e => e.Times)

SELECT e.Title, SUM(e.Times) AS TotalTimes

FROM Employee AS e

GROUP BY e.Title

from e in Employee

group e by e.Title into g

select new {Title = g.Key, TotalTimes = g.Sum(e => e.Times)}

Employee

.GroupBy(e => e.Title)

.Select(

g => new {Title = g.Key, TotalTimes = g.Sum(e => e.Times)}

)

 

SELECT e.Title, SUM(e.Times) AS TotalTimes

FROM Employee AS e

GROUP BY e.Title

HAVING COUNT(*) > 1

 

from e in Employee

group e by e.Title into g

where g.Count() > 1

select new {Title = g.Key, TotalTimes = g.Sum(e => e.Times)}

 Employee

.GroupBy(e => e.Title)

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

.Select(

g => new {Title = g.Key, TotalTimes = g.Sum(e => e.Times)}

)

SELECT *

FROM Product AS p

INNER JOIN Employee AS e

ON p.EmployeeID = e.ID

from p in Product

join e in Employee

on p.EmployeeID equals e.ID

select new {p, e}

SELECT p.EmployeeID

FROM Product AS p

UNION

SELECT e.ID

FROM Employee AS e

(from p in Product

select p.EmployeeID).Union(

from e in Employee

select e.ID)

Product.Select(p => p.EmployeeID).Union(

Employee.Select(e => e.ID)

)

SELECT TOP (10) *

FROM Product AS p

WHERE p.EmployeeID = 1

(from p in Product

where p.EmployeeID = 1

select p).Take(10)

Product
.Where(p => (p.EmployeeID = 1))
.Take(10)

SELECT *

FROM Product AS p

WHERE p.EmployeeID IN(

    SELECT e.ID

    FROM Employee AS e

    WHERE e.Flag = 1

)

from p in Product

where (from e in Employee

    where e.Flag == 1

    select e.ID).Contains(p.EmployeeID)

select p

Product

.Where(

    p => Employee

    .Where(e => (e.Flag == 1))

    .Select(e => e.ID).Contains(p.EmployeeID)

)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值