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