首页 > 其他分享 >linq与lambda对照

linq与lambda对照

时间:2022-09-06 08:44:18浏览次数:53  
标签:pr JobTitle Employees 对照 linq new lambda SELECT ProductID

linq与lambda对照

C#开发中linq与lambda写法对照

SQL LINQ Lambda
SELECT * FROM Employee from e in Employees select e Employees.Select ()
SELECT e.LoginID,e.JobTitle FROM 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 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 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 e Employees.Where (e => (e.LoginID == “test”))
SELECT e.* FROM 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 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 Employee AS e ORDER BY e.NationalIDNumber from e in Employees orderby e.NationalIDNumber select e Employees.OrderBy (e => e.NationalIDNumber)
SELECT e.* FROM 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 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 Employee AS e Employees.Sum(e => e.VacationHours);
SELECT COUNT(*) FROM Employee AS e Employees.Count();
SELECT SUM(e.VacationHours) AS TotalVacations,e.JobTitle FROM 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 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 Product AS p,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 Product AS p INNER JOIN 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 Product AS p INNER JOIN 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 Product AS p LEFT OUTER JOIN 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 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 p Products.Where (p =>ProductReviews.Where (pr => (pr.Rating == 5)) .Select (pr => pr.ProductID).Contains (p.ProductID)

linq与lambda写法对照

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

标签:pr,JobTitle,Employees,对照,linq,new,lambda,SELECT,ProductID
From: https://www.cnblogs.com/ouyangkai/p/16660389.html

相关文章

  • Lambda表达式
    ......
  • linq小结
    普通查询varquery=fromsincontext.Studentselects;//投影列varquery=fromsincontext.Studentselectnew{s.Id,s.StudentName};//起别名varquer......
  • 一篇文章教你学会ASP.Net Core LINQ基本操作
    一篇文章教你学会ASP.NetCoreLINQ基本操作为什么要使用LINQLINQ中提供了很多集合的扩展方法,配合lambda能简化数据处理。例如我们想要找出一个IEnumerable<int>中所有......
  • [AWS] Lambda Python Get Current Account Id
    UnlikeAWS_REGIONorAWS_LAMBDA_FUNCTION_NAME,wecannotgetcurrentaccountidfromtheenvironmentvariables.Inordertotogettheaccountid,wecanuse......
  • 函数和lambda表达式
    函数定义与传参机制deffuncName(ParaList): """ DescriptionText """ #Body ... return函数的形参类型位置参数关键字参数FuncName(para1=...)#......
  • C#并行编程:PLINQ
    PLINQ可以自动并行化本地LINQ查询。易于使用是PLINQ的优势,因为它将工作划分和结果整理的任务交给了.NETCore。要使用PLINQ,只需直接在输入序列上调用AsParallel()方法,而后......
  • JAVA进阶--常用时间API、包装类、正则表达式、Array类、Lambda表达式、常见算法--202
    第一节 Date日期对象1、日期对象如何创建,如何获取时间毫秒值Datedate=newDate();Longtime=date.getTime();2、时间毫秒值怎么恢复成......
  • 莫兰迪色系色卡RGB对照表
    【转】莫兰迪色系色卡RGB对照表莫兰迪色系,广义上是指饱和度低、带有灰调的颜色系列,大家也常把它称为“高级灰”。近年流行的雾蓝、烟粉、豆绿、冷咖等,都是由莫兰迪色系所......
  • C++ 之lambda表达式
    C++之lambda表达式[capture](parameters)option->return-type{body}capture:捕获列表,可选捕捉列表总是出现在lambda表达式的开始。实际上,是lambda引出符,编译......
  • LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria
    LINQ:WhentouseSingleOrDefaultvs.FirstOrDefault()withfilteringcriteria问题   回答1Ifyourresultsetreturns0records:SingleOrDefaultretu......