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 (

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

时间: 2024-10-12 12:06:45

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

LINQ TO SQL Linq入门基础

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

sql linq lambda 对比

1. 查询Student表中的所有记录的Sname.Ssex和Class列. select sname,ssex,class from student Linq: from s in Students select new { s.SNAME, s.SSEX, s.CLASS } Lambda: Students.Select( s => new { SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS }) 2. 查询教师所有的单位即不重复的Depart列

LINQ / LINQ to SQL / LINQ to XXX 它们到底有什么区别

今天有幸被召回母校给即将毕业的学弟学妹们讲我这两年的工作史,看了下母校没啥特别的变化,就是寝室都安了空调,学妹们都非常漂亮而已..好了不扯蛋了,说下今天的主题吧.这些天我在深度定制语法高亮功能的同时发现了博客园提供的一些有意思的函数,甚至有几个博客园都没用到,我也不知道怎么才能触发那些功能..打开这个js就可以看到很多好用的东西了,虽然写的不怎么样,但是至少有这些功能. ps: 推荐安装一个代码格式化的插件,否则一坨看着蛋疼.比如第一个就是 log,方便调试. www.qdmm.com/Book

最新的VS学习视频网站:ASP.NET, MVC, SQL , LINQ, WebService,等 http://csharp-video-tutorials.blogspot.jp/

最近开发项目经常会碰到一些开发问题,基于Windows开发人员,强烈推荐. http://csharp-video-tutorials.blogspot.jp/

LINQ to SQL语句Concat/Union/Intersect/Except--2017年2月22日

Concat/Union/Intersect/Except操作 适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1.简单形式: var q = ( from c in db.Customers select c.Phone ).Concat( from c in db.Customers select c.Fax ).Concat( from e in db.Employees select e.Home

linq to sql转载

LINQ简介 LINQ:语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作内存数据. LINQ是一门查询语言,和SQL一样,通过一些关键字的组合,实现最终的查询. LINQ的分类 LINQ to ObjectLINQ to XMLLINQ to SQLLINQ to DataSetLINQ to ADO.NET 命名空间为System.Linq; LINQ查

详解Linq to SQL

第一部分,什么是Linq to sql Linq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并提供了丰富的查询功能,它和Linq to xml.Linq to objects.Linq to dataset.Linq to entities等组成了强大的LINQ. 要学好LINQ查询语法,就不得不先理解C# 3.0的一些新特性,下面一一简单介绍. 第二部分,隐含类型局部变量 var age

Linq之Linq to Sql

目录 写在前面 系列文章 Linq to sql 总结 写在前面 上篇文章介绍了linq to xml的相关内容,linq to xml提供一种更便捷的创建xml树,及查询的途径.这篇文章将继续介绍linq to sql的内容.个人觉得linq to sql在实际开发中在中小型项目中用的比较多,在中小型项目用ef或者nhibernate这些orm确实有点重量级.Linq to Sql提供了丰富的功能,完全可以满足日常数据访问的需求.使用方法也非常简单.灵活. 系列文章 Linq之Lambda表达