1. 江湖人士首页
  2. IT江湖

C#开发中linq与lambda写法对照,收藏好这1个表格哟

C#开发中linq与lambda写法对照,在使用C#编程过程中,有些码农在实现查询功能的时候纠结于到底使用linq还是lambda来写代码,有些还搞不清楚,现就两者之间写汗的比较整理一个表格供参考。

C#开发中linq与lambda写法对照,在使用C#编程过程中,有些码农在实现查询功能的时候纠结于到底使用linq还是lambda来写代码,有些还搞不清楚,现就两者之间写汗的比较整理一个表格供参考,具体表格如下。linq与lambda写法对照对于新手来说有点用。

linq与lambda写法对照
linq与lambda写法对照

linq与lambda写法对照

SQLLINQLambda
SELECT * FROM Employeefrom e in Employees select eEmployees.Select ()
SELECT e.LoginID,e.JobTitle FROM Employee AS efrom 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 Employee AS efrom 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 Employee AS e(from e in Employees select e.JobTitle).Distinct()Employees.Select (e => e.JobTitle)
.Distinct ()
SELECT e.* FROM Employee AS e WHERE e.LoginID = ‘test’from e in Employees where e.LoginID == “test” select eEmployees.Where (e => (e.LoginID == “test”))
SELECT e.* FROM Employee AS e WHERE e.LoginID = ‘test’ AND e.SalariedFlag = 1from e in Employees where e.LoginID == “test” && e.SalariedFlag select eEmployees.Where (e => ((e.LoginID == “test”) && e.SalariedFlag))
SELECT e.* FROM Employee AS e WHERE e.VacationHours >= 2 AND e.VacationHours <= 10from e in Employees where e.VacationHours >= 2 && e.VacationHours <= 10 select eEmployees.Where (e => (((Int32)(e.VacationHours) >= 2) && ((Int32)(e.VacationHours) <= 10)))
SELECT e.* FROM Employee AS e ORDER BY e.NationalIDNumberfrom e in Employees orderby e.NationalIDNumber select eEmployees.OrderBy (e => e.NationalIDNumber)
SELECT e.* FROM Employee AS e ORDER BY e.HireDate DESC, e.NationalIDNumberfrom e in Employees orderby e.HireDate descending, e.NationalIDNumber select eEmployees.OrderByDescending (e => e.HireDate).ThenBy (e => e.NationalIDNumber)
SELECT e.* FROM 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 eEmployees.Where (e => (e.JobTitle.StartsWith (“Vice”) || (e.JobTitle.Substring (0, 3) == “Pro”)))
SELECT SUM(e.VacationHours) FROM Employee AS eEmployees.Sum(e => e.VacationHours);
SELECT COUNT(*) FROM Employee AS eEmployees.Count();
SELECT SUM(e.VacationHours) AS TotalVacations,e.JobTitle FROM Employee AS e GROUP BY e.JobTitlefrom 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 Employee AS e GROUP BY e.JobTitle HAVING e.COUNT(*) > 2from 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 Product AS p,ProductReview AS prfrom p in Products from pr in ProductReviews select new {p, pr}Products.SelectMany (p => ProductReviews,(p, pr) =>new{
p = p,
pr = pr})
SELECT * FROM Product AS p INNER JOIN ProductReview AS pr ON p.ProductID = pr.ProductIDfrom 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 Product AS p INNER JOIN ProductCostHistory AS pch ON p.ProductID = pch.ProductID AND p.SellStartDate = pch.StartDatefrom 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 Product AS p LEFT OUTER JOIN ProductReview AS pr ON p.ProductID = pr.ProductIDfrom 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 Product AS p UNION SELECT pr.ProductReviewID FROM 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 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 [Product] AS p WHERE p.ProductID IN (SELECT pr.ProductID FROM [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 pProducts.Where (p =>ProductReviews.Where (pr => (pr.Rating == 5)) .Select (pr => pr.ProductID).Contains (p.ProductID)
linq与lambda写法对照

linq与lambda写法对照可以让你在写linq或者lambda时,更加的方便。

本站最新优惠

Namesilo优惠:新用户省 $1 域名注册-优惠码:45D%UYTcxYuCloZ 国外最便宜域名!点击了解更多

特别优惠:免费赠送 $100 Vultr主机-限时优惠!英文站必备海外服务器!点击了解更多

VPS优惠:搬瓦工优惠码:BWH3OGRI2BMW 最高省5.83%打开外面世界的一款主机点击了解更多

原创文章,作者:江小编,如若转载,请注明出处:https://jhrs.com/2018/12525.html

扫码关注【江湖人士】公众号,您会获得关于国外被动收入的最新资讯

WA付费会员QQ群:387027533,加这个群需要回答您的WA会员名,待核实后予以通过

普通QQ交流群:178758794,可分享交流建站的各类经验和知识

发表评论

登录后才能评论