SQL/LINQ/Lambda 转换
2013-01-18 12:11
253 查看
一、lamda 简单说就是个语法糖。
本来你要声明实现一个方法,然后在调用,要写一大堆代码,可是微软总是认为程序员是懒散的,如二楼的例子:
Func<int,int,int> fc=(int x,int y)=>x*y;
你看就一句代码,声明一个方法,返回值为int,参数是两个int。功能是计算两个整数的乘积。
二、LINQ是一种用来进行数据访问的编程模型,使得.NET语言可以直接支持数据查询。
–LINQ to Objects 用于对象的查询–LINQ to XML 对XML数据的查询
–LINQ to ADO.NET 对数据库的查询
•LINQ to DataSets 数据集
•LINQ to Entities ORM对象
•LINQ to SQL 简易ORM框架
实现 IEnumerable<T> 或 IQueryable<T> 接口的对象都可使用LINQ操作。
三、SQL / LINQ / Lambda 对应列表
SQL | LINQ | Lambda |
---|---|---|
FROM | ||
SELECT * FROM Employees | from e in Employees select e | Employees .Select (e => e) |
SELECT e.LoginID, e.JobTitle FROM Employees 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 Employees AS e | from e in Employees select new {ID = e.LoginID, Title = e.JobTitle} | Employees .Select ( e => new { ID = e.LoginID, Title = e.JobTitle } ) |
DISTINCT 用来过滤掉多余的重复记录只保留一条 | ||
SELECT DISTINCT e.JobTitle FROM Employee AS e | (from e in Employees select e.JobTitle).Distinct() | Employees .Select (e => e.JobTitle) .Distinct () |
WHERE | ||
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))) |
ORDER BY | ||
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) |
LIKE | ||
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"))) |
SUM | ||
SELECT SUM(e.VacationHours) FROM Employee AS e | | Employees.Sum(e => e.VacationHours); |
COUNT | ||
SELECT COUNT(*) FROM HumanResources.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 [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) ) |
相关文章推荐
- Sql->Linq-> Lambda 相互转换
- 使用 LINQPad 将linq转换为 lambda表达式 或者 SQL语句
- SQL,Linq,Lambda之间的转换练习
- LINQ体验(13)——LINQ to SQL语句之运算符转换和ADO.NET与LINQ to SQL
- sql 、linq、Lambda 查询语句的格式
- LINQ体验(13)——LINQ to SQL语句之运算符转换和ADO.NET与LINQ to SQL
- sql语句-linq语言-lambda表达式对照 (赞)
- 隨機排序或選取n筆資料(使用SQL、LINQ與Lambda)
- sql 、linq、lambda 查询语句的区别
- sql语句-linq语言-lambda表达式对照 分类: .NET 2014-08-16 04:02 250人阅读 评论(0) 收藏
- sql语句-linq语言-lambda表达式对照
- SQL、LINQ、Lambda 三种用法(转)
- [转]SQL,LINQ,Lambda语法对照图
- Linq to SQL 中将数字转换为字符串
- Linq To sql入门练习 Lambda表达式基础
- Linq To Sql中实现Left Join与Inner Join使用Linq语法与lambda表达式
- Linq to Sql lambda例子总结
- LINQ to SQL语句(18)之运算符转换
- 将Linq转换为SQL的便捷方法
- sql语句-linq语言-lambda表达式对照