Differences between SQL Linq lamda and sqllinqlamda

Source: Internet
Author: User

Differences between SQL Linq lamda and sqllinqlamda

SQL

LINQ

Lambda

SELECT *

FROM HumanResources. Employee

From e in Employees

Select e

Employees
. Select (e => e)

SELECT e. LoginID, e. JobTitle

FROM HumanResources. Employee 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 Title

FROM HumanResources. Employee AS e

From e in Employees

Select new {ID = e. LoginID, Title = e. JobTitle}

Employees
. Select (
E =>
New
{
ID = e. LoginID,
Title = e. JobTitle
}
)

Select distinct e. JobTitle

FROM HumanResources. Employee AS e

(From e in Employees

Select e. JobTitle). Distinct ()

Employees
. Select (e => e. JobTitle)
. Distinct ()

SELECT e .*

FROM HumanResources. Employee AS e

WHERE e. LoginID = 'test'

From e in Employees

Where e. LoginID = "test"

Select e

Employees
. Where (e => (e. LoginID = "test "))

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
. Where (e => (e. LoginID = "test") & e. SalariedFlag ))

SELECT e .*
FROM HumanResources. Employee AS e

WHERE e. VacationHours> = 2 AND e. VacationHours <= 10

From e in Employees

Where e. VacationHours> = 2 & e. VacationHours <= 10

Select e

Employees
. Where (e => (Int32) (e. VacationHours)> = 2) & (Int32) (e. VacationHours) <= 10 )))

SELECT e .*

FROM HumanResources. Employee AS e
Order by e. NationalIDNumber

From e in Employees

Orderby e. NationalIDNumber

Select e

Employees
. OrderBy (e => e. NationalIDNumber)

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
. OrderByDescending (e => e. HireDate)
. ThenBy (e => e. NationalIDNumber)

SELECT e .*
FROM HumanResources. Employee 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 ")))

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
. GroupBy (e => e. JobTitle)
. Select (
G =>
New
{
JobTitle = g. Key,
TotalVacations = g. Sum (e => (Int32) (e. VacationHours ))
}
)

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
. GroupBy (e => e. JobTitle)
. Where (g => (g. Count ()> 2 ))
. Select (
G =>
New
{
JobTitle = g. Key,
TotalVacations = g. Sum (e => (Int32) (e. VacationHours ))
}
)

SELECT *

FROM Production. Product AS p, Production. ProductReview AS pr

From p in Products

From pr in ProductReviews

Select new {p, pr}

Products
. SelectMany (
P => ProductReviews,
(P, pr) =>
New
{
P = p,
Pr = pr
}
)

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
. Join (
ProductReviews,
P => p. ProductID,
Pr => pr. ProductID,
(P, pr) =>
New
{
P = p,
Pr = pr
}
)

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
. Join (
ProductCostHistories,
P =>
New
{
ProductID = p. ProductID,
StartDate = p. SellStartDate
},
Pch =>
New
{
ProductID = pch. ProductID,
StartDate = pch. StartDate
},
(P, pch) =>
New
{
P = p,
Pch = pch
}
)

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
. GroupJoin (
ProductReviews,
P => p. ProductID,
Pr => pr. ProductID,
(P, prodrev) =>
New
{
P = p,
Prodrev = prodrev
}
)

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 (
P =>
New
{
ID = p. ProductID
}
)
. Union (
ProductReviews
. Select (
Pr =>
New
{
ID = pr. ProductReviewID
}
)
)

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
. Where (p => (p. StandardCost <100 ))
. Take (10)

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
. Where (
P =>
ProductReviews
. Where (pr => (pr. Rating = 5 ))
. Select (pr => pr. ProductID)
. Contains (p. ProductID)
)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.