浅谈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 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

实例 Code

 
 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 条件过滤

实例 Code

 
查询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.排序

实例 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 whereclass=''
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 查询语句的区别的更多相关文章

  1. 浅谈sql之连接查询

    SQL之连接查询 一.连接查询的分类 sql中将连接查询分成四类: 内链接 外连接 左外连接 右外连接 自然连接 交叉连接 二.连接查询的分类 数据库表如下: 1.学生表 2.老师表 3.班级表 表用 ...

  2. sql 、linq、lambda 查询语句的区别

    LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量中被查询的值 [group by 条件] Lambda ...

  3. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

  4. 转【】浅谈sql中的in与not in,exists与not exists的区别_

    浅谈sql中的in与not in,exists与not exists的区别   1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表 ...

  5. 浅谈sql中的in与not in,exists与not exists的区别

    转 浅谈sql中的in与not in,exists与not exists的区别   12月12日北京OSC源创会 —— 开源技术的年终盛典 »   sql exists in 1.in和exists ...

  6. 【SqlServer系列】浅谈SQL Server事务与锁(上篇)

    一  概述 在数据库方面,对于非DBA的程序员来说,事务与锁是一大难点,针对该难点,本篇文章视图采用图文的方式来与大家一起探讨. “浅谈SQL Server 事务与锁”这个专题共分两篇,上篇主讲事务及 ...

  7. 浅谈SQL Server内部运行机制

    对于已经很熟悉T-SQL的读者,或者对于较专业的DBA来说,逻辑的增删改查,或者较复杂的SQL语句,都是非常简单的,不存在任何挑战,不值得一提,那么,SQL的哪些方面是他们的挑战 或者软肋呢? 那就是 ...

  8. 浅谈SQL Server数据内部表现形式

    在上篇文章 浅谈SQL Server内部运行机制 中,与大家分享了SQL Server内部运行机制,通过上次的分享,相信大家已经能解决如下几个问题: 1.SQL Server 体系结构由哪几部分组成? ...

  9. 浅谈SQL Server---2

    浅谈SQL Server内部运行机制 https://www.cnblogs.com/wangjiming/p/10098061.html 对于已经很熟悉T-SQL的读者,或者对于较专业的DBA来说, ...

随机推荐

  1. Flex4 DataGrid ItemRenderer内嵌方式

    Flex4 DataGrid ItemRenderer像Flex3一直内嵌ItemRenderer会报空对象引用的错误,如: <s:GridColumn dataField="titl ...

  2. Objective-C运行时编程 - 方法混写 Method Swizzling

    摘要: 本文描述方法混写对实例.类.父类.不存在的方法等情况处理,属于Objective-C(oc)运行时(runtime)编程范围. 编程环境:Xcode 6.1.1, Yosemite,iOS 8 ...

  3. vector和iterator及collection

    Collection是所有集合的最上层接口,它里面定义了所有集合对象都可以进行的操作:它有两个子接口,分别是List和Set.List会记录放在其中元素的放入顺序,形象地说,可以认为是一个传送带,它上 ...

  4. Softmax 回归原理介绍

    考虑一个多分类问题,即预测变量y可以取k个离散值中的任何一个.比如一个邮件分类系统将邮件分为私人邮件,工作邮件和垃圾邮件.由于y仍然是一个离散值,只是相对于二分类的逻辑回归多了一些类别.下面将根据多项 ...

  5. CodeForces 163A Substring and Subsequence dp

    A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...

  6. win7远程桌面连接

    远程桌面连接 1.在防火墙上设置同意同意远程桌面通过防火墙: 2.为管理员设置password.以便訪问者訪问: 3.我的电脑-右键属性-远程设置-远程-选上"同意远程协助远程这台计算机.同 ...

  7. 各种ORM安装

    1.EF安装 2.PetaPoco安装 Install-Package PetaPoco 3.

  8. Soft Renderer的乐趣

    最近这一个月的闲暇时间在边学习<3D编程大师技巧>边做自己的Soft Renderer,一个月下来总算有了个“基本原型”的样子.主要是在编写图形管线的过程式代码,简单明了为第一个阶段的目标 ...

  9. 用Java实现向Cassandra数据库中插入和查询数据

    所用jar包: 其中jxl.jar和dom4j.jar,jaxen-1.1-beta-6.jar是解析XML文件用的jar包,如果不解析XML文件可以不用. 代码如下: package com.loc ...

  10. 动态引入Js文件

    var src = "/Scripts/Test.js"; $("<script type = 'text/javascript' src='" + sr ...