SQL、LINQ、Lambda 三种用法互换

SQL、LINQ、Lambda 三种用法
颜色注释: SQL LinqToSql Lambda QA
1、 查询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
})

2、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher
Linq:
from t in Teachers.Distinct()
select t.DEPART
Lambda:
Teachers.Distinct().Select( t => t.DEPART)
3、 查询Student表的所有记录。
select * from student
Linq:
from s in Students
select s
Lambda:
Students.Select( s => s)

4、 查询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、 查询Score表中成绩为85,86或88的记录。
select * from score where degree in (85,86,88)
Linq:
In
from s in Scores
where (
new decimal[]{85,86,88}
).Contains(s.DEGREE)
select s
Lambda:
Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))
Not in
from s in Scores
where !(
new decimal[]{85,86,88}
).Contains(s.DEGREE)
select s
Lambda:
Scores.Where( s => !(new Decimal[]{85,86,88}.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
6、 查询Student表中"95031"班或性别为"女"的同学记录。
select * from student where class =‘95031‘ 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、 以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)

9、 查询"95031"班的学生人数。
select count(*) from student where class = ‘95031‘
Linq:
( from s in Students
where s.CLASS == "95031"
select s
).Count()
Lambda:
Students.Where( s => s.CLASS == "95031" )
.Select( s => s)
.Count()
10、查询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()
11、查询‘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()

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(degree) from score where cno like ‘3%‘ group by Cno having count(*)>=5
Linq:
from s in Scores
where s.CNO.StartsWith("3")
group s by s.CNO
into cc
where cc.Count() >= 5
select cc.Average( c => c.DEGREE)
Lambda:
Scores.Where( s => s.CNO.StartsWith("3") )
.GroupBy( s => s.CNO )
.Where( cc => ( cc.Count() >= 5) )
.Select( cc => cc.Average( c => c.DEGREE) )
Linq: SqlMethod
like也可以这样写:
s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")
13、查询最低分大于70,最高分小于90的Sno列。
select sno from score group by sno having min(degree) > 70 and max(degree) < 90
Linq:
from s in Scores
group s by s.SNO
into ss
where ss.Min(cc => cc.DEGREE) > 70 && ss.Max( cc => cc.DEGREE) < 90
select new
{
sno = ss.Key
}
Lambda:
Scores.GroupBy (s => s.SNO)
.Where (ss => ((ss.Min (cc => cc.DEGREE) > 70) && (ss.Max (cc => cc.DEGREE) < 90)))
.Select ( ss => new {
sno = ss.Key
})
14、查询所有学生的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
})
15、查询所有学生的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
})
16、查询所有学生的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 }

时间: 2024-08-07 19:15:40

SQL、LINQ、Lambda 三种用法互换的相关文章

SQL、LINQ、Lambda 三种用法(转)

SQL.LINQ.Lambda 三种用法 颜色注释: SQL LinqToSql Lambda QA 1. 查询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 =

SQL、LINQ、Lambda 三种用法

使用工具:LINQPad4 1.准备数据表   Customer(顾客表)-- Id(自增主键)Name(顾客姓名)Phone(顾客联系方式) CREATE TABLE [dbo].[Customer]( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NULL, [Phone] [nvarchar](16) NULL, CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED ( [Id] A

指针与引用的区别以及引用的三种用法

1.指针与引用的区别: 指针是一块内存的地址值,而引用是一块内存的别名. 下面引自:http://www.cnblogs.com/kingln/articles/1129114.html 从概念上讲.指针从本质上讲就是存放变量地址的一个变量,在逻辑上是独立的,它可以被改变,包括其所指向的地址的改变和其指向的地址中所存放的数据的改变. 而引用是一个别名,它在逻辑上不是独立的,它的存在具有依附性,所以引用必须在一开始就被初始化,而且其引用的对象在其整个生命周期中是不能被改变的(自始至终只能依附于同一

防止sql注入的三种方法

常用的避免SQL注入的三种方法 一,存储过程 在学习数据库视频的时候接触过,它是存储在数据库中的一些事先编译好的指令.在用的时候不用重新编写,直接调用就好了.所以,使用它可以大大提高程序的执行效率. 那么,如果创建一个存储程序并使用它呢?这是我们今天要解决的问题 1.创建过程 可编程性--下拉菜单--存储过程--右键--查询菜单--指定模板参数的值--新建查询--输入语句--查询菜单中的分析检查语法是否正确--执行 2.具体创建语法 在创建存储程序时,为了应对各种变换的数据,通常会涉及到带参数的

java中 this 的三种用法

Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 (3)this调用构造方法只能在本构造方法中调用另一个构造方法(4)this 调用构造方法必须写在第一行 eg: 1 public class ThisDemo { 2 private int id; 3 private String name; 4 public ThisDemo(){ //(1)

c# new的三种用法

在 C# 中,new 关键字可用作运算符.修饰符或约束. 1)new 运算符:用于创建对象和调用构造函数.这种大家都比较熟悉,没什么好说的了. 2)new 修饰符:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员. 3)new 约束:用于在泛型声明中约束可能用作类型参数的参数的类型. 东西不用就会忘c# new的三种用法,布布扣,bubuko.com

.NET(c#)new关键字的三种用法

前几天去家公司面试,有一道这样的题:写出c#中new关键字的三种用法,思前想后挖空心思也只想出了两种用法,回来查了下msdn,还真是有第三种用法:用于在泛型声明中约束可能用作类型参数的参数的类型,这是在Framework 2.0 中定义泛行时才会使用到的,自己对c# 2.0 中的支持还只是粗通皮毛,怪不得累死so many脑细胞也没能想不出这第三种来! 三种用法如下: 在 C# 中,new 关键字可用作运算符.修饰符或约束. 1)new 运算符:用于创建对象和调用构造函数.这种大家都比较熟悉,没

this和super关键字的常见三种用法

super和this的含义 super :代表父类的存储空间标识(可以理解为父亲的引用). this :代表当前对象的引用(谁调用就代表谁). super关键字的三种用法: 在子类的成员方法中,访问父类的成员变量格式:super.成员变量名: 在子类的成员方法中,访问父类的成员方法格式:super.方法变量名: 在子类的构造方法中,访问父类的构造方法格式:super(): this关键字的三种用法: 在本类成员方法中,访问本类的成员变量格式:this.变量名 在本类成员方法中,访问本类的另一个成

PDO中执行SQL语句的三种方法

在PDO中,我们可以使用三种方式来执行SQL语句,分别是 exec()方法,query方法,以及预处理语句prepare()和execute()方法~大理石构件来图加工 在上一篇文章<使用PDO构造函数连接数据库及DSN详解>中,我们介绍了如何使用构造函数连接数据库和DSN的详解,那么我们这篇文章跟大家介绍在PDO中执行SQL语句的三种方式,下面我们将一一介绍! 第一种方法:exec()方法 exec()方法返回执行SQL 语句后受影响的行数,其语法格式如下: 1 int PDO::exec(