Sql-Linq-Lambda对照表

 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)

)

posted @ 2017-08-29 19:23  by-lhc  阅读(182)  评论(0编辑  收藏  举报