SQL/LINQ/Lamda 写法[转发]


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)
   )

转发自:http://www.cnblogs.com/babietongtianta/p/3192967.html

时间: 2024-11-19 12:26:53

SQL/LINQ/Lamda 写法[转发]的相关文章

SQL /LINQ/Lamda

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 selectnew {e.LoginID, e.JobTitle} Employees    .Select

复杂Linq语句写法

从网上收藏的复杂Linq语句写法 1.case when: 原型: sql原型: SELECT ProductID, Name, Color, CASE WHEN Color = 'Red' THEN StandardCost WHEN Color = 'Black' THEN StandardCost + 10 ELSE ListPrice END Price FROM SalesLT.Product Linq代码: Products.Select(P => new { ID = P.Prod

LAMP下的PHP执行sql语句的写法

LAMP下的PHP执行sql语句的写法: 代码如下: @mysql_connect("localhost", "root","1981427") //选择数据库之前需要先连接数据库服务器 or die("数据库服务器连接失败"); @mysql_select_db("test") //选择数据库mydb or die("数据库不存在或不可用"); $query = @mysql_quer

java.sql.SQLException: 对只转发结果集的无效操作: last

出错代码如下:static String u = "user";static String p = "psw";static String url = "jdbc:oracle:thin:@localhost:1521:db_name";con = DriverManager.getConnection(url, u, p); statement = con.createStatement();String query1="select

C#DataTable 使用GroupBy方法的lamda 表达式和Linq语句写法

https://www.cnblogs.com/johnblogs/p/6006867.html DataTable ds = new DataTable(); //1.lamda 表达式写法(推荐) var result = ds.AsEnumerable().GroupBy(s => new{Year = s.Field<int>("Year"), Month = s.Field<int>("Month"), Day = s.Fie

LINQ TO SQL Linq入门基础

我们以一个酒店管理系统的数据库为例子 表结构很简单:GuestInfo(客人信息表),Room(房间表),RoomType(房间类型表),HotelInfo(每个月盈利信息) 整体就是,Room中有个房间类型ID的外键,客人表中有个房间的ID的外键.月盈利信息先不管 准备:新建项目 linq_Ch4控制台程序,新建DB文件夹 右击DB文件夹,添加→新建项 选择LINQ to SQL类,文件名为 Hotel.dbml,添加 打开服务器资源管理器,我的快捷键是Ctrl+W+L 选中这四张表,往已经打

关于报表sql报表的写法

我在我的项目中发现基本上所有的报表 都是三个界面 总 我发现这三级页面的查询语句 有共用的部分,但是之前该系统的这写报表每一级页面都是是单独写查询并且直接写在jsp页面中,我们经理写了新的方法将sql语句放到单独文件去读取,但是往前的程序员没有去改,而是复制粘贴前面的写法,我细细的看了一次经理的写法后,然后细看了下api发现还有许多好的方法,但是在项目中没有用,我觉得之前的同事如果细看一下该会提高多少效率和避免多少错误,于是我把它整理出来了,因为思路是可以共用的直接上图了 这只是第一级页面的写法

Linq表达式写法

Linq表达式,实现按照某个字段排序的简单写法. 做项目的时候遇到的一个简单问题,于是记下来. 列举一个例子: <T> model=new <T>(); 加入model中有要根据字段主键id,然后部门id=1排序. Linq写法: var quary=from r in model where r.部门id=1 orderBy r.主键id select r; foreach(model r in quary) { Console.WriteLiner(r); }

SQL注入详解-转发

转发自飞天小子如有侵权请联系删除 1:什么是SQL注入 SQL注入是一种将SQL代码插入或添加到应用(用户)的输入参数中的攻击,之后再将这些参数传递给后台的SQL服务器加以解析并执行. www.xx.com/news.php?id=1 www.xx.com/news.php?id=1 and 1=1 这里我们来理解一下SQL注入 首先,SQL注入常年蝉联OWASP排行榜第一名~ SQL注入产生的过程是怎样的呢?见下图 SQL注入的危害有哪些呢? 数据库信息泄露 网页篡改 网站被挂马 数据库被恶意