sql linq lambda 对比
、 查询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 对比的更多相关文章
- SQL,LINQ,Lambda语法对照图(转载)
如果你熟悉SQL语句,当使用LINQ时,会有似曾相识的感觉.但又略有不同.下面是SQL和LINQ,Lambda语法对照图 SQL LINQ Lambda SELECT * FROM HumanReso ...
- SQL,Linq,Lambda之间的转换练习
1.查询Student表中的所有记录的Sname.Ssex和Class列. SQL:select sname,ssex,class from Students linq:from s in Stude ...
- SQL/LINQ/Lamda 写法[转发]
SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees .Sele ...
- SQL Linq lamda区别
SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees .Sele ...
- SQL/LINQ/Lamda
SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees .Sele ...
- ASP.NET EF(LINQ/Lambda查询)
EF(EntityFrameWork) ORM(对象关系映射框架/数据持久化框架),根据实体对象操作数据表中数据的一种面向对象的操作框架,底层也是调用ADO.NET ASP.NET MVC 项目会自动 ...
- mysql与sql server参照对比学习mysql
mysql与sql server参照对比学习mysql 关键词:mysql语法.mysql基础 转自桦仔系列:http://www.cnblogs.com/lyhabc/p/3691555.html ...
- [算法1-排序](.NET源码学习)& LINQ & Lambda
[算法1-排序](.NET源码学习)& LINQ & Lambda 说起排序算法,在日常实际开发中我们基本不在意这些事情,有API不用不是没事找事嘛.但必要的基础还是需要了解掌握. 排 ...
- sql,lambda,linq语句
实例 Code 查询Student表的所有记录. select * from student Linq: from s in Students select s Lambda: Students.Se ...
随机推荐
- 配置基于NotePad++工具下的C#开发环境
1.打开NotePad++,打开Notepad++的插件(plugins)菜单-->Plugin Manager-->Show Plugin Manager-->勾选NppExec- ...
- OC——网络解析获取图片的应用
headimageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, DEVW, DEVW/2)]; headimageView.cont ...
- override和new的区别
override 1. override是派生类用来重写基类中方法的: 2. override不能重写非虚方法和静态方法: 3. override只能重写用virtual.abstract.overr ...
- celery 使用multiprocessing 问题记录
报错: [2013-11-29 14:27:48,297: ERROR/MainProcess] Task app.add[e5d184c0-471f-4fc4-804c-f760178d4847] ...
- AngularJS 之Services讲解
Angular带来了很多类型的services.每个都会它自己不同的使用场景.我们将在本节来阐述. 首先我们必须记在心里的是所有的services都是singleton(单例)的,这也是我们所希望得到 ...
- plsql基本语法(
1. 定义常量的语法格式 常量名 constant 类型标识符 [not null]:=值; 常量,包括后面的变量名都必须以字母开头,不能有空格,不能超过30个字符长度,同时不能和保留字同 ...
- API例子:用Java/JavaScript下载内容提取器
1,引言 本文讲解怎样用Java和JavaScript使用 GooSeeker API 接口下载内容提取器,这是一个示例程序.什么是内容提取器?为什么用这种方式?源自Python即时网络爬虫开源项目: ...
- JVM GC之一找出不可达对象并回收
JAVA运行时数据区域 1.程序计数器:当前线程所执行的字节码的行号指示器.一个处理器只会执行一条线程中的指令,为了线程切换后能回复到正确的执行位置,所以每条线程都需要一个独立的计数器.各条线程之间互 ...
- Nginx的HTTP模块
1.HTTP的核心模块.这些HTTP模块会在编译Nginx时自动编译进来,除非使用configure命令禁止编译这些模块.(1)alias指令.该指令用于在URL和文件系统路径之间实现映射.它与roo ...
- android activity启动的时候隐藏软键盘
1.概述 android如果界面有EditText之类的跳软键盘的控件 在跳转到该界面是默认会跳出软键盘的. 更何况有些需求要直接需要获取焦点 <requestFocus /> 如果是E ...