浅谈sql 、linq、lambda 查询语句的区别
浅谈sql 、linq、lambda 查询语句的区别
LINQ的书写格式如下:
from 临时变量 in 集合对象或数据库对象
where 条件表达式
[order by条件]
select 临时变量中被查询的值
[group by 条件]
Lambda表达式的书写格式如下:
(参数列表) => 表达式或者语句块
其中: 参数个数:可以有多个参数,一个参数,或者无参数。
参数类型:可以隐式或者显式定义。
表达式或者语句块:这部分就是我们平常写函数的实现部分(函数体)。
1.查询全部
查询Student表的所有记录。
select * from student
Linq:
from s in Students
select s
Lambda:
Students.Select( s => s)
2 按条件查询全部:
查询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 去掉重复的
查询教师所有的单位即不重复的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
查询Score表中成绩在60到80之间的所有记录。
select * from score where degree between 60 and 80
Linq:
from s in Scores
where s.DEGREE >=60&& s.DEGREE <80
select s
Lambda:
Scores.Where(
s => (
s.DEGREE >=60&& s.DEGREE <80
)
)
5.在范围内筛选 In
select * from score where degree in (85,86,88)
Linq:
from s in Scores
where (
newdecimal[]{85,86,88}
).Contains(s.DEGREE)
select s
Lambda:
Scores.Where( s =>new Decimal[] {85,86,88}.Contains(s.DEGREE))
6.or 条件过滤
查询Student表中"95031"班或性别为"女"的同学记录。
select * from student whereclass='' or ssex= N'女'
Linq:
from s in Students
where s.CLASS =="95031"
|| s.CLASS =="女"
select s
Lambda:
Students.Where(s => ( s.CLASS =="95031"|| s.CLASS =="女"))
7.排序
以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()行数查询
select count(*) from student whereclass=''
Linq:
( from s in Students
where s.CLASS ==""
select s
).Count()
Lambda:
Students.Where( s => s.CLASS =="" )
.Select( s => s)
.Count()
10.avg()平均
查询'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.子查询
查询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.分组 过滤
查询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.分组
查询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之连接查询
SQL之连接查询 一.连接查询的分类 sql中将连接查询分成四类: 内链接 外连接 左外连接 右外连接 自然连接 交叉连接 二.连接查询的分类 数据库表如下: 1.学生表 2.老师表 3.班级表 表用 ...
- sql 、linq、lambda 查询语句的区别
LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量中被查询的值 [group by 条件] Lambda ...
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程
c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...
- 转【】浅谈sql中的in与not in,exists与not exists的区别_
浅谈sql中的in与not in,exists与not exists的区别 1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表 ...
- 浅谈sql中的in与not in,exists与not exists的区别
转 浅谈sql中的in与not in,exists与not exists的区别 12月12日北京OSC源创会 —— 开源技术的年终盛典 » sql exists in 1.in和exists ...
- 【SqlServer系列】浅谈SQL Server事务与锁(上篇)
一 概述 在数据库方面,对于非DBA的程序员来说,事务与锁是一大难点,针对该难点,本篇文章视图采用图文的方式来与大家一起探讨. “浅谈SQL Server 事务与锁”这个专题共分两篇,上篇主讲事务及 ...
- 浅谈SQL Server内部运行机制
对于已经很熟悉T-SQL的读者,或者对于较专业的DBA来说,逻辑的增删改查,或者较复杂的SQL语句,都是非常简单的,不存在任何挑战,不值得一提,那么,SQL的哪些方面是他们的挑战 或者软肋呢? 那就是 ...
- 浅谈SQL Server数据内部表现形式
在上篇文章 浅谈SQL Server内部运行机制 中,与大家分享了SQL Server内部运行机制,通过上次的分享,相信大家已经能解决如下几个问题: 1.SQL Server 体系结构由哪几部分组成? ...
- 浅谈SQL Server---2
浅谈SQL Server内部运行机制 https://www.cnblogs.com/wangjiming/p/10098061.html 对于已经很熟悉T-SQL的读者,或者对于较专业的DBA来说, ...
随机推荐
- Vehicle Network Protocols -- ISO/KWP CAN CCD PCI SCI / SCP / Class 2
Vehicle Network Protocols There are 5 protocols in the OBD2 system and a car will normally only use ...
- 用一个I/O口控制1个三色指示灯, 2个单色指示灯
http://www.baiheee.com/Documents/081207/081207184434.htm http://www.baiheee.com/Documents/081207/081 ...
- Linux文件时间属性
Linux文件时间属性 ...
- 浅谈Androidclient项目框架
写Android也有些时间了,一边工作,一边学习,一边积累.仅仅有遇到问题了,花时间去研究,自己的能力才干提升.刀假设不用.慢慢的就会生锈应该也是这个道理吧!上个月公司项目server框架进行的一些调 ...
- Java和C++中多态的实现方式
多态是面向对象的最主要的特性之一,是一种方法的动态绑定,实现运行时的类型决定对象的行为.多态的表现形式是父类指针或引用指向子类对象,在这个指针上调用的方法使用子类的实现版本.多态是IOC.模板模式实现 ...
- 9款风格华丽的jQuery/CSS3插件
今天向大家分享9款效果相当不错的jQuery/CSS3插件,不多说,直接来看看这些插件吧. 1.jQuery动画下拉菜单Smart Menu 这是一款基于jQuery的动画下拉菜单,子菜单外观比较时尚 ...
- 多条件判断语句case
一.case语句的基本格式: case 变量 in 模式1) 语句块1 :: 模式2) 语句块2 :: ...... :: esac 上面的格式中,每个模式后面的两个分号"::"是 ...
- 管理和维护RHCS集群
导读 管理和维护RHCS集群是一个非常复杂和繁琐的工作,要维护好一个RHCS集群,必须熟悉RHCS的基本运行原理,在集群管理方面,RHCS提供了两种方式:即Luci图形界面方式和命令行方式,这儿重点讲 ...
- 项目源码--Android迷幻岛屿综合游戏
下载源码 技术要点: 1.游戏开发综合技术 2.多线程机制实现游戏逻辑 3.自定义控件,系统控件等综合图层的使用 4.图层素材动画的综合技术 5.游戏算法的实现 6. OpenGL ES的综合使用 7 ...
- jquery . fancybox()
1.父页面 function chooseTopic(btn) {//选择议题 $.fancybox({ type : 'iframe', href : '', fitToView : false, ...