、 查询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
}) 、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher
Linq:
from t in Teachers.Distinct()
select t.DEPART
Lambda:
Teachers.Distinct().Select( t => t.DEPART) 、 查询Student表的所有记录。
select * from student
Linq:
from s in Students
select s
Lambda:
Students.Select( s => s) 、 查询Score表中成绩在60到80之间的所有记录。
select * from score where degree between and
Linq:
from s in Scores
where s.DEGREE >= && s.DEGREE <
select s
Lambda:
Scores.Where(
s => (
s.DEGREE >= && s.DEGREE <
)
) 、 查询Score表中成绩为85,86或88的记录。
select * from score where degree in (,,)
Linq:
In
from s in Scores
where (
new decimal[]{,,}
).Contains(s.DEGREE)
select s
Lambda:
Scores.Where( s => new Decimal[] {,,}.Contains(s.DEGREE))
Not in
from s in Scores
where !(
new decimal[]{,,}
).Contains(s.DEGREE)
select s
Lambda:
Scores.Where( s => !(new Decimal[]{,,}.Contains(s.DEGREE))) Any()应用:双表进行Any时,必须是主键为(String)
CustomerDemographics CustomerTypeID(String)
CustomerCustomerDemos (CustomerID CustomerTypeID) (String)
一个主键与二个主建进行Any(或者是一对一关键进行Any)
不可,以二个主键于与一个主键进行Any from e in CustomerDemographics
where !e.CustomerCustomerDemos.Any()
select e from c in Categories
where !c.Products.Any()
select c 、 查询Student表中""班或性别为"女"的同学记录。
select * from student where class ='' or ssex= N'女'
Linq:
from s in Students
where s.CLASS == ""
|| s.CLASS == "女"
select s
Lambda:
Students.Where(s => ( s.CLASS == "" || s.CLASS == "女")) 、 以Class降序查询Student表的所有记录。
select * from student order by Class DESC
Linq:
from s in Students
orderby s.CLASS descending
select s
Lambda:
Students.OrderByDescending(s => s.CLASS) 、 以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by Cno ASC,Degree DESC
Linq:(这里Cno ASC在linq中要写在最外面)
from s in Scores
orderby s.DEGREE descending
orderby s.CNO ascending
select s
Lambda:
Scores.OrderByDescending( s => s.DEGREE)
.OrderBy( s => s.CNO) 、 查询""班的学生人数。
select count(*) from student where class = ''
Linq:
( from s in Students
where s.CLASS == ""
select s
).Count()
Lambda:
Students.Where( s => s.CLASS == "" )
.Select( s => s)
.Count() 、查询Score表中的最高分的学生学号和课程号。
select distinct s.Sno,c.Cno from student as s,course as c ,score as sc
where s.sno=(select sno from score where degree = (select max(degree) from score))
and c.cno = (select cno from score where degree = (select max(degree) from score))
Linq:
(
from s in Students
from c in Courses
from sc in Scores
let maxDegree = (from sss in Scores
select sss.DEGREE
).Max()
let sno = (from ss in Scores
where ss.DEGREE == maxDegree
select ss.SNO).Single().ToString()
let cno = (from ssss in Scores
where ssss.DEGREE == maxDegree
select ssss.CNO).Single().ToString()
where s.SNO == sno && c.CNO == cno
select new {
s.SNO,
c.CNO
}
).Distinct()
操作时问题?执行时报错: where s.SNO == sno(这行报出来的) 运算符"=="无法应用于"string"和"System.Linq.IQueryable<string>"类型的操作数
解决:
原:let sno = (from ss in Scores
where ss.DEGREE == maxDegree
select ss.SNO).ToString()
Queryable().Single()返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
解:let sno = (from ss in Scores
where ss.DEGREE == maxDegree
select ss.SNO).Single().ToString() 、查询'3-105'号课程的平均分。
select avg(degree) from score where cno = '3-105'
Linq:
(
from s in Scores
where s.CNO == "3-105"
select s.DEGREE
).Average()
Lambda:
Scores.Where( s => s.CNO == "3-105")
.Select( s => s.DEGREE)
.Average() 、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(degree) from score where cno like '3%' group by Cno having count(*)>=
Linq:
from s in Scores
where s.CNO.StartsWith("")
group s by s.CNO
into cc
where cc.Count() >=
select cc.Average( c => c.DEGREE)
Lambda:
Scores.Where( s => s.CNO.StartsWith("") )
.GroupBy( s => s.CNO )
.Where( cc => ( cc.Count() >= ) )
.Select( cc => cc.Average( c => c.DEGREE) )
Linq: SqlMethod
like也可以这样写:
s.CNO.StartsWith("") or SqlMethods.Like(s.CNO,"%3") 、查询最低分大于70,最高分小于90的Sno列。
select sno from score group by sno having min(degree) > and max(degree) <
Linq:
from s in Scores
group s by s.SNO
into ss
where ss.Min(cc => cc.DEGREE) > && ss.Max( cc => cc.DEGREE) <
select new
{
sno = ss.Key
}
Lambda:
Scores.GroupBy (s => s.SNO)
.Where (ss => ((ss.Min (cc => cc.DEGREE) > ) && (ss.Max (cc => cc.DEGREE) < )))
.Select ( ss => new {
sno = ss.Key
}) 、查询所有学生的Sname、Cno和Degree列。
select s.sname,sc.cno,sc.degree from student as s,score as sc where s.sno = sc.sno
Linq:
from s in Students
join sc in Scores
on s.SNO equals sc.SNO
select new
{
s.SNAME,
sc.CNO,
sc.DEGREE
}
Lambda:
Students.Join(Scores, s => s.SNO,
sc => sc.SNO,
(s,sc) => new{
SNAME = s.SNAME,
CNO = sc.CNO,
DEGREE = sc.DEGREE
}) 、查询所有学生的Sno、Cname和Degree列。
select sc.sno,c.cname,sc.degree from course as c,score as sc where c.cno = sc.cno
Linq:
from c in Courses
join sc in Scores
on c.CNO equals sc.CNO
select new
{
sc.SNO,c.CNAME,sc.DEGREE
}
Lambda:
Courses.Join ( Scores, c => c.CNO,
sc => sc.CNO,
(c, sc) => new
{
SNO = sc.SNO,
CNAME = c.CNAME,
DEGREE = sc.DEGREE
}) 、查询所有学生的Sname、Cname和Degree列。
select s.sname,c.cname,sc.degree from student as s,course as c,score as sc where s.sno = sc.sno and c.cno = sc.cno
Linq:
from s in Students
from c in Courses
from sc in Scores
where s.SNO == sc.SNO && c.CNO == sc.CNO
select new { s.SNAME,c.CNAME,sc.DEGREE }

转自CSDN:http://blog.csdn.net/swarb/article/details/8206976

sql linq lambda 对比的更多相关文章

  1. SQL,LINQ,Lambda语法对照图(转载)

    如果你熟悉SQL语句,当使用LINQ时,会有似曾相识的感觉.但又略有不同.下面是SQL和LINQ,Lambda语法对照图 SQL LINQ Lambda SELECT * FROM HumanReso ...

  2. SQL,Linq,Lambda之间的转换练习

    1.查询Student表中的所有记录的Sname.Ssex和Class列. SQL:select sname,ssex,class from Students linq:from s in Stude ...

  3. SQL/LINQ/Lamda 写法[转发]

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  4. SQL Linq lamda区别

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  5. SQL/LINQ/Lamda

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  6. ASP.NET EF(LINQ/Lambda查询)

    EF(EntityFrameWork) ORM(对象关系映射框架/数据持久化框架),根据实体对象操作数据表中数据的一种面向对象的操作框架,底层也是调用ADO.NET ASP.NET MVC 项目会自动 ...

  7. mysql与sql server参照对比学习mysql

    mysql与sql server参照对比学习mysql 关键词:mysql语法.mysql基础 转自桦仔系列:http://www.cnblogs.com/lyhabc/p/3691555.html ...

  8. [算法1-排序](.NET源码学习)& LINQ & Lambda

    [算法1-排序](.NET源码学习)& LINQ & Lambda 说起排序算法,在日常实际开发中我们基本不在意这些事情,有API不用不是没事找事嘛.但必要的基础还是需要了解掌握. 排 ...

  9. sql,lambda,linq语句

    实例 Code 查询Student表的所有记录. select * from student Linq: from s in Students select s Lambda: Students.Se ...

随机推荐

  1. 从一段代码看fork()函数及其引发的竞争

    首先来看一段从<UNIX环境高级编程>中摘录的一段很有意思的代码.借此我们再来谈谈fork()函数的一些问题. #include "apue.h" static voi ...

  2. 关于vnc连接闪退问题的设置

    设置如下: 依次点Option-->Advanced-->Expert找到ColourLevel,默认值是pal8,修改为rgb222或full.

  3. JS知识点概况

    1.什么是JavaScript a)   JavaScript 被设计用来向 HTML 页面添加交互行为. b)   JavaScript 是一种脚本语言(脚本语言是一种轻量级的编程语言). c)   ...

  4. iOS FMDB

    FMDB FMDB概述 什么是FMDB * FMDB是iOS平台的SQLite数据库框架 * FMDB以OC的方式封装了SQLite的C语言API FMDB的优点 * 使用起来更加面向对象,省去了很多 ...

  5. SDK编程模板

    #include<Windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain(HINS ...

  6. linux系统学习笔记:文件、目录、用户

    本篇主要从stat函数开始,逐个说明stat结构的每一个成员,以此来了解文件的所有属性.同时将说明修改这个属性的各个函数. 一.文件 使用stat函数族得到和文件有关的信息结构. #include & ...

  7. 入门指引 - PHP手册笔记

    曾经简单的学习过PHP,看的是<PHP和MySQL Web开发>,还有万能的搜索引擎的帮助.这次准备系统的学习一下,参考资料是PHP Manual. PHP能做什么 PHP主要用于服务端的 ...

  8. SQL Server identity种子

    背景: 用identity修饰列可以使它自动增长 例了: create table T(ID int not null identity(1,1),      Data nvarchar(32)); ...

  9. U盘开发之安全U盘

    普通型安全U盘,虚拟KEY和U盘两个设备,由主机软件分别对KEY和U盘进行操作,U盘与上位机采用usb mass storage接口,KEY采用HID接口,两者均无需驱动.也有虚拟成光盘和U盘两个设备 ...

  10. 纯CSS美化的checkbox 和 radio

    html <!DOCTYPE HTML> <html> <head> <title>纯CSS3实现自定义美化复选框和单选框</title> ...