sql 、linq、lambda 查询语句的区别
LINQ的书写格式如下:
from 临时变量 in 集合对象或数据库对象
where 条件表达式
[order by条件]
select 临时变量中被查询的值
[group by 条件]
Lambda表达式的书写格式如下:
(参数列表) => 表达式或者语句块
其中: 参数个数:可以有多个参数,一个参数,或者无参数。
参数类型:可以隐式或者显式定义。
表达式或者语句块:这部分就是我们平常写函数的实现部分(函数体)。
1.查询全部
实例 Code
查询Student表的所有记录。
select * from student
Linq:
from s in Students
select s
Lambda:
Students.Select( s => s)
2 按条件查询全部:
实例 Code
查询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
})
3.distinct 去掉重复的
实例 Code
查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher
Linq:
from t in Teachers.Distinct()
select t.DEPART
Lambda:
Teachers.Distinct().Select( t => t.DEPART)
4.连接查询 between and
实例 Code
查询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 <
)
)
5.在范围内筛选 In
实例 Code
select * from score where degree in (,,)
Linq:
from s in Scores
where (
new decimal[]{,,}
).Contains(s.DEGREE)
select s
Lambda:
Scores.Where( s => new Decimal[] {,,}.Contains(s.DEGREE))
6.or 条件过滤
实例 Code
查询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 == "女"))
7.排序
实例 Code
以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)
8.count()行数查询
实例 Code
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()
10.avg()平均
实例 Code
查询'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)
11.子查询
实例 Code
查询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()
12.分组 过滤
实例 Code
查询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")
13.分组
实例 Code
查询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")
14. 多表查询
实例 Code
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
})
.Average()
sql 、linq、lambda 查询语句的区别的更多相关文章
- 浅谈sql 、linq、lambda 查询语句的区别
浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...
- SQL,LINQ,Lambda语法对照图(转载)
如果你熟悉SQL语句,当使用LINQ时,会有似曾相识的感觉.但又略有不同.下面是SQL和LINQ,Lambda语法对照图 SQL LINQ Lambda SELECT * FROM HumanReso ...
- ASP.NET EF(LINQ/Lambda查询)
EF(EntityFrameWork) ORM(对象关系映射框架/数据持久化框架),根据实体对象操作数据表中数据的一种面向对象的操作框架,底层也是调用ADO.NET ASP.NET MVC 项目会自动 ...
- SQL结构化查询语句
SQL结构化查询语句 SQL定义了查询所有关系型数据库的规则. 1.通用语法 SQL语句可以单行或者多行书写,以分号结尾 可以使用空格和缩进增强可读性 不区分大小写,但是关键字建议大写 3种注释 注释 ...
- linq字符串搜索条件,排序条件-linq动态查询语句 Dynamic LINQ
在做搜索和排序的时候,往往是前台传过来的字符串做条件,参数的数量还不定,这就需要用拼sql语句一样拼linq语句.而linq语句又是强类型的,不能用字符串拼出来. 现在好了,有个开源的linq扩展方法 ...
- SQL Server-简单查询语句,疑惑篇(三)
前言 对于一些原理性文章园中已有大量的文章尤其是关于索引这一块,我也是花费大量时间去学习,对于了解索引原理对于后续理解查询计划和性能调优有很大的帮助,而我们只是一些内容进行概括和总结,这一节我们开始正 ...
- sql的基本查询语句
--------------------------------------------基本常用查询-------------------------------------- 自己简单练习做了个表. ...
- SQL Server-简单查询语句,疑惑篇
前言 对于一些原理性文章园中已有大量的文章尤其是关于索引这一块,我也是花费大量时间去学习,对于了解索引原理对于后续理解查询计划和性能调优有很大的帮助,而我们只是一些内容进行概括和总结,这一节我们开 ...
- sql 中联合查询语句
在查询语句中 两张表进行查询,可以通过 left join (左连接查询) :返回左表中的所有记录和右表中联结字段相等的记录 (意思就是左表中的数据会全部显示,右表中只会显示和左表中相等的字段) r ...
随机推荐
- IOS第十天(1:QQ好友列表 ,自定义的headview,代理 ,通知 ,black的使用)
*****HMViewController.m #import "HMViewController.h" #import "HMFriendsGroupModel.h&q ...
- 【iCore3 双核心板_FPGA】例程九:状态机实验——状态机使用
实验指导书及代码包下载: http://pan.baidu.com/s/1o72i3mq iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- 【iCore3 双核心板_FPGA】实验二十:基于FIFO的ARM+FPGA数据存取实验
实验指导书及代码包下载: http://pan.baidu.com/s/1cmisnO iCore3 购买链接: https://item.taobao.com/item.htm?id=5242294 ...
- ArcGIS Server 创建站点失败
前期解决方案中部分解决方法汇总:①安装Server时创建的ArcGIS Server Account (操作系统级别用户,默认用户名arcgis)对创建站点时新建的站点目录arcgisserver文件 ...
- Android 图文数据JSON解析
数据格式为 {"sid":"737","tts":"http:\/\/news.iciba.com\/admin\/tts\/20 ...
- ASP.NET Page执行顺序【转】
一.ASP.NET 母版页和内容页中的事件 母版页和内容页都可以包含控件的事件处理程序.对于控件而言,事件是在本地处理的,即内容页中的控件在内容页中引发事件,母版页中的控件在母版页中引发事件.控件事件 ...
- c#中关键词out和ref的区别
c#中关键词out和ref用来表明以传引用的方式传递参数. 区别如下: 如果方法的参数用out标记,表示方法被调用前不需初始化参数,方法内不能读取此参数的值,在方法返回前必须向此参数写入值: 如果方法 ...
- Extjs 图片的自动缩放
function resizeImage(obj) { var width = Ext.getCmp('welcome').getWidth(); //welcome 为一Panel的id 分割线下的 ...
- Java提高篇——Java实现多重继承
多重继承指的是一个类可以同时从多于一个的父类那里继承行为和特征,然而我们知道Java为了保证数据安全,它只允许单继承.有些时候我们会认为如果系统中需要使用多重继承往往都是糟糕的设计,这个时候我们往往需 ...
- ID3决策树的Java实现
package DecisionTree; import java.io.*; import java.util.*; public class ID3 { //节点类 public class DT ...