搜索
首页后端开发C#.Net教程SQL、LINQ和Lambda表达式

SQL、LINQ和Lambda表达式

Feb 23, 2017 am 10:05 AM
lambda


首先说说这三者完全是三种不同的东西,SQL是结构化查询语言(Structured Query Language)简称,这大家再熟悉不过了,下面主要介绍LINQ和Lambda表达式的基本概念以及同一查询这三者的不同实现。

简单介绍

LINQ(Language Integrate Query)是语言集成查询他在对象和数据之间建立一种对应的关系,可以使用访问内存对象的方式查询数据集合。LINQ查询是C#中的一种语言构造。因此开发人员可以再C#代码汇总嵌套类似于SQL语句的查询表达式,从而实现数据查询的功能。LINQ也不是简单地作为C#中嵌套查询表达式,而是将查询表达式作为C#的一种语法。
 在.NET类库中,LINQ相关类库都在System.Linq命名空间下,该命名空间提供支持使用LINQ进行查询的类和接口,其中最主要的是以下两个类和两个接口。
 ※IEnumerable接口:它表示可以查询的数据集合,一个查询通常是逐个对集合中的元素进行筛选操作,返回一个新的IEnumerable接口,用来保存查询结果。
 ※IQueryable接口:他继承IEnumerable接口,表示一个可以查询的表达式目录树。
 ※Enumerable类:它通过对IEnumerable提供扩展方法,实现LINQ标准查询运算符。包括过路、导航、排序、查询、联接、求和、求最大值、求最小值等操作。
 ※Queryable类:它通过对IQueryable提供扩展方法,实现LINQ标准查询运算符。包括过路、导航、排序、查询、联接、求和、求最大值、求最小值等操作。
 Lambda表达式实际上是一个匿名函数,它可以说是对LINQ的补充。由于LINQ查询关键字和IEnumerable接口的方法之间有一个对应关系,但是LINQ查询表达式中可以使用的查询功能很少。在实际开发中通过查询结果或数据源进行方法调用,从而进行更多的查询操作。由于Lambda表达式是匿名函数,它可以赋值到一个委托,而在IEnumerable接口的方法中很多通过函数委托来实现自定义运算、条件等操作,所以Lambda表达式在LINQ中被广泛使用。

对比实现

※查询全部内容

1 查询Student表的所有记录。
2 select * from student
3 Linq:
4     from s in Students
5     select s
6 Lambda:
7     Students.Select( s => s)

※按列查询

select sname,ssex,class from student 
3 Linq: 
4     from s in Students 
5     select new { 
6         s.SNAME, 
7         s.SSEX, 
8         s.CLASS
 9     }
 10 Lambda:
 11     Students.Select( s => new {
 12         SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS
 13     })

※distinct去重查询

 查询教师所有的单位即不重复的Depart列。
2 select distinct depart from teacher
3 Linq:
4     from t in Teachers.Distinct()
5     select t.DEPART6 Lambda:
7     Teachers.Distinct().Select( t => t.DEPART)

※两个区间内查询

1 查询Score表中成绩在60到80之间的所有记录。
 2 select * from score where degree between 60 and 80
 3 Linq: 
 4     from s in Scores 
 5     where s.DEGREE >= 60 && s.DEGREE < 80
 6     select s 
 7 Lambda: 
 8     Scores.Where( 
 9         s => (
 10                 s.DEGREE >= 60 && s.DEGREE < 80
 11              )
 12     )

※在一个范围内查询

 select * from score where degree in (85,86,88)
 2 Linq:
 3     from s in Scores
 4     where (
 5             new decimal[]{85,86,88}
 6           ).Contains(s.DEGREE)
 7     select s
 8 Lambda:
 9     Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))

※或关系查询

 查询Student表中"95031"班或性别为"女"的同学记录。
 2 select * from student where class =&#39;95031&#39; or ssex= N&#39;女&#39;
 3 Linq:
 4     from s in Students
 5     where s.CLASS == "95031"
 6        || s.CLASS == "女"
 7     select s
 8 Lambda:
 9     Students.Where(s => ( s.CLASS == "95031" || s.CLASS == "女"))

※排序

 以Class降序查询Student表的所有记录。
 2 select * from student order by Class DESC
 3 Linq:
 4     from s in Students
 5     orderby s.CLASS descending
 6     select s
 7 Lambda:
 8     Students.OrderByDescending(s => s.CLASS)

※行数查询

 select count(*) from student where class = &#39;95031&#39;
 2 Linq: 
 3     (    from s in Students 
 4         where s.CLASS == "95031"
 5         select s 
 6     ).Count() 
 7 Lambda: 
 8     Students.Where( s => s.CLASS == "95031" ) 
 9                 .Select( s => s)
 10                     .Count()

※平均值查询

 查询&#39;3-105&#39;号课程的平均分。
 2 select avg(degree) from score where cno = &#39;3-105&#39;
 3 Linq: 
 4     ( 
 5         from s in Scores 
 6         where s.CNO == "3-105"
 7         select s.DEGREE 
 8     ).Average() 
 9 Lambda:
 10     Scores.Where( s => s.CNO == "3-105")
 11             .Select( s => s.DEGREE)

※潜逃查询

查询Score表中的最高分的学生学号和课程号。
 2 select distinct s.Sno,c.Cno from student as s,course as c ,score as sc 
 3 where s.sno=(select sno from score where degree = (select max(degree) from score)) 
 4 and c.cno = (select cno from score where degree = (select max(degree) from score)) 
 5 Linq: 
 6     ( 
 7         from s in Students 
 8         from c in Courses 
 9         from sc in Scores
 10         let maxDegree = (from sss in Scores
 11                         select sss.DEGREE
 12                         ).Max()
 13         let sno = (from ss in Scores
 14                 where ss.DEGREE == maxDegree
 15                 select ss.SNO).Single().ToString()
 16         let cno = (from ssss in Scores
 17                 where ssss.DEGREE == maxDegree
 18                 select ssss.CNO).Single().ToString()
 19         where s.SNO == sno && c.CNO == cno
 20         select new {
 21             s.SNO,
 22             c.CNO
 23         }
 24     ).Distinct()

※分组

 查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
 2 select avg(degree) from score where cno like &#39;3%&#39; group by Cno having count(*)>=5
 3 Linq: 
 4         from s in Scores 
 5         where s.CNO.StartsWith("3") 
 6         group s by s.CNO 
 7         into cc 
 8         where cc.Count() >= 5
 9         select cc.Average( c => c.DEGREE)
 10 Lambda:
 11     Scores.Where( s => s.CNO.StartsWith("3") )
 12             .GroupBy( s => s.CNO )
 13               .Where( cc => ( cc.Count() >= 5) )
 14                 .Select( cc => cc.Average( c => c.DEGREE) )
 15 Linq: SqlMethod
 16 like也可以这样写:
 17     s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")

※分组过滤

查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
 2 select avg(degree) from score where cno like &#39;3%&#39; group by Cno having count(*)>=5
 3 Linq: 
 4         from s in Scores 
 5         where s.CNO.StartsWith("3") 
 6         group s by s.CNO 
 7         into cc 
 8         where cc.Count() >= 5
 9         select cc.Average( c => c.DEGREE)
 10 Lambda:
 11     Scores.Where( s => s.CNO.StartsWith("3") )
 12             .GroupBy( s => s.CNO )
 13               .Where( cc => ( cc.Count() >= 5) )
 14                 .Select( cc => cc.Average( c => c.DEGREE) )
 15 Linq: SqlMethod
 16 like也可以这样写:
 17     s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")

※多表联合查询

 select sc.sno,c.cname,sc.degree from course as c,score as sc where c.cno = sc.cno
 2 Linq: 
 3     from c in Courses 
 4     join sc in Scores 
 5     on c.CNO equals sc.CNO
 6     select new 
 7     { 
 8         sc.SNO,c.CNAME,sc.DEGREE
 9     }
 10 Lambda:
 11     Courses.Join ( Scores, c => c.CNO,
 12                              sc => sc.CNO,
 13                              (c, sc) => new
 14                                         {
 15                                             SNO = sc.SNO,
 16                                             CNAME = c.CNAME,
 17                                             DEGREE = sc.DEGREE
 18                                         })
 19                 .Average()

以上内容是本人查阅资料做的一些整理和总结,有不足之处请大家批评指正!

首先说说这三者完全是三种不同的东西,SQL是结构化查询语言(Structured Query Language)简称,这大家再熟悉不过了,下面主要介绍LINQ和Lambda表达式的基本概念以及同一查询这三者的不同实现。

简单介绍

LINQ(Language Integrate Query)是语言集成查询他在对象和数据之间建立一种对应的关系,可以使用访问内存对象的方式查询数据集合。LINQ查询是C#中的一种语言构造。因此开发人员可以再C#代码汇总嵌套类似于SQL语句的查询表达式,从而实现数据查询的功能。LINQ也不是简单地作为C#中嵌套查询表达式,而是将查询表达式作为C#的一种语法。
 在.NET类库中,LINQ相关类库都在System.Linq命名空间下,该命名空间提供支持使用LINQ进行查询的类和接口,其中最主要的是以下两个类和两个接口。
 ※IEnumerable接口:它表示可以查询的数据集合,一个查询通常是逐个对集合中的元素进行筛选操作,返回一个新的IEnumerable接口,用来保存查询结果。
 ※IQueryable接口:他继承IEnumerable接口,表示一个可以查询的表达式目录树。
 ※Enumerable类:它通过对IEnumerable提供扩展方法,实现LINQ标准查询运算符。包括过路、导航、排序、查询、联接、求和、求最大值、求最小值等操作。
 ※Queryable类:它通过对IQueryable提供扩展方法,实现LINQ标准查询运算符。包括过路、导航、排序、查询、联接、求和、求最大值、求最小值等操作。
 Lambda表达式实际上是一个匿名函数,它可以说是对LINQ的补充。由于LINQ查询关键字和IEnumerable接口的方法之间有一个对应关系,但是LINQ查询表达式中可以使用的查询功能很少。在实际开发中通过查询结果或数据源进行方法调用,从而进行更多的查询操作。由于Lambda表达式是匿名函数,它可以赋值到一个委托,而在IEnumerable接口的方法中很多通过函数委托来实现自定义运算、条件等操作,所以Lambda表达式在LINQ中被广泛使用。

对比实现

※查询全部内容

1 查询Student表的所有记录。
2 select * from student
3 Linq:
4     from s in Students
5     select s
6 Lambda:
7     Students.Select( s => s)

※按列查询

select sname,ssex,class from student 
3 Linq: 
4     from s in Students 
5     select new { 
6         s.SNAME, 
7         s.SSEX, 
8         s.CLASS
 9     }
 10 Lambda:
 11     Students.Select( s => new {
 12         SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS
 13     })

※distinct去重查询

 查询教师所有的单位即不重复的Depart列。
2 select distinct depart from teacher
3 Linq:
4     from t in Teachers.Distinct()
5     select t.DEPART
6 Lambda:
7     Teachers.Distinct().Select( t => t.DEPART)

※两个区间内查询

1 查询Score表中成绩在60到80之间的所有记录。
 2 select * from score where degree between 60 and 80
 3 Linq: 
 4     from s in Scores 
 5     where s.DEGREE >= 60 && s.DEGREE < 80
 6     select s 
 7 Lambda: 
 8     Scores.Where( 
 9         s => (
 10                 s.DEGREE >= 60 && s.DEGREE < 80
 11              )
 12     )

※在一个范围内查询

 select * from score where degree in (85,86,88)
 2 Linq:
 3     from s in Scores
 4     where (
 5             new decimal[]{85,86,88}
 6           ).Contains(s.DEGREE)
 7     select s
 8 Lambda:
 9     Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))

※或关系查询

 查询Student表中"95031"班或性别为"女"的同学记录。
 2 select * from student where class =&#39;95031&#39; or ssex= N&#39;女&#39;
 3 Linq:
 4     from s in Students
 5     where s.CLASS == "95031"
 6        || s.CLASS == "女"
 7     select s
 8 Lambda:
 9     Students.Where(s => ( s.CLASS == "95031" || s.CLASS == "女"))

※排序

 以Class降序查询Student表的所有记录。
 2 select * from student order by Class DESC
 3 Linq:
 4     from s in Students
 5     orderby s.CLASS descending
 6     select s
 7 Lambda:
 8     Students.OrderByDescending(s => s.CLASS)

※行数查询

 select count(*) from student where class = &#39;95031&#39;
 2 Linq: 
 3     (    from s in Students 
 4         where s.CLASS == "95031"
 5         select s 
 6     ).Count() 
 7 Lambda: 
 8     Students.Where( s => s.CLASS == "95031" ) 
 9                 .Select( s => s)
 10                     .Count()

※平均值查询

 查询&#39;3-105&#39;号课程的平均分。
 2 select avg(degree) from score where cno = &#39;3-105&#39;
 3 Linq: 
 4     ( 
 5         from s in Scores 
 6         where s.CNO == "3-105"
 7         select s.DEGREE 
 8     ).Average() 
 9 Lambda:
 10     Scores.Where( s => s.CNO == "3-105")
 11             .Select( s => s.DEGREE)

※潜逃查询

查询Score表中的最高分的学生学号和课程号。
 2 select distinct s.Sno,c.Cno from student as s,course as c ,score as sc 
 3 where s.sno=(select sno from score where degree = (select max(degree) from score)) 
 4 and c.cno = (select cno from score where degree = (select max(degree) from score)) 
 5 Linq: 
 6     ( 
 7         from s in Students
 8         from c in Courses 
 9         from sc in Scores
 10         let maxDegree = (from sss in Scores
 11                         select sss.DEGREE
 12                         ).Max()
 13         let sno = (from ss in Scores
 14                 where ss.DEGREE == maxDegree
 15                 select ss.SNO).Single().ToString()
 16         let cno = (from ssss in Scores
 17                 where ssss.DEGREE == maxDegree
 18                 select ssss.CNO).Single().ToString()
 19         where s.SNO == sno && c.CNO == cno
 20         select new {
 21             s.SNO,
 22             c.CNO
 23         }
 24     ).Distinct()

※分组

 查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
 2 select avg(degree) from score where cno like &#39;3%&#39; group by Cno having count(*)>=5
 3 Linq: 
 4         from s in Scores 
 5         where s.CNO.StartsWith("3") 
 6         group s by s.CNO 
 7         into cc 
 8         where cc.Count() >= 5
 9         select cc.Average( c => c.DEGREE)
 10 Lambda:
 11     Scores.Where( s => s.CNO.StartsWith("3") )
 12             .GroupBy( s => s.CNO )
 13               .Where( cc => ( cc.Count() >= 5) )
 14                 .Select( cc => cc.Average( c => c.DEGREE) )
 15 Linq: SqlMethod
 16 like也可以这样写:
 17     s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")

※分组过滤

查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
 2 select avg(degree) from score where cno like &#39;3%&#39; group by Cno having count(*)>=5
 3 Linq: 
 4         from s in Scores 
 5         where s.CNO.StartsWith("3") 
 6         group s by s.CNO 
 7         into cc 
 8         where cc.Count() >= 5
 9         select cc.Average( c => c.DEGREE)
 10 Lambda:
 11     Scores.Where( s => s.CNO.StartsWith("3") )
 12             .GroupBy( s => s.CNO )
 13               .Where( cc => ( cc.Count() >= 5) )
 14                 .Select( cc => cc.Average( c => c.DEGREE) )
 15 Linq: SqlMethod
 16 like也可以这样写:
 17     s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")

※多表联合查询

 select sc.sno,c.cname,sc.degree from course as c,score as sc where c.cno = sc.cno
 2 Linq: 
 3     from c in Courses 
 4     join sc in Scores 
 5     on c.CNO equals sc.CNO
 6     select new 
 7     { 
 8         sc.SNO,c.CNAME,sc.DEGREE
 9     }
 10 Lambda:
 11     Courses.Join ( Scores, c => c.CNO,
 12                              sc => sc.CNO,
 13                              (c, sc) => new
 14                                         {
 15                                             SNO = sc.SNO,
 16                                             CNAME = c.CNAME,
 17                                             DEGREE = sc.DEGREE
 18                                         })
 19                 .Average()

 以上就是SQL、LINQ和Lambda表达式的内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
C#.NET:强大的编程语言简介C#.NET:强大的编程语言简介Apr 22, 2025 am 12:04 AM

C#和.NET的结合为开发者提供了强大的编程环境。1)C#支持多态性和异步编程,2).NET提供跨平台能力和并发处理机制,这使得它们在桌面、Web和移动应用开发中广泛应用。

.NET框架与C#:解码术语.NET框架与C#:解码术语Apr 21, 2025 am 12:05 AM

.NETFramework是一个软件框架,C#是一种编程语言。1..NETFramework提供库和服务,支持桌面、Web和移动应用开发。2.C#设计用于.NETFramework,支持现代编程功能。3..NETFramework通过CLR管理代码执行,C#代码编译成IL后由CLR运行。4.使用.NETFramework可快速开发应用,C#提供如LINQ的高级功能。5.常见错误包括类型转换和异步编程死锁,调试需用VisualStudio工具。

揭开c#.net的神秘面纱:初学者的概述揭开c#.net的神秘面纱:初学者的概述Apr 20, 2025 am 12:11 AM

C#是一种由微软开发的现代、面向对象的编程语言,.NET是微软提供的开发框架。C#结合了C 的性能和Java的简洁性,适用于构建各种应用程序。.NET框架支持多种语言,提供垃圾回收机制,简化内存管理。

C#和.NET运行时:它们如何一起工作C#和.NET运行时:它们如何一起工作Apr 19, 2025 am 12:04 AM

C#和.NET运行时紧密合作,赋予开发者高效、强大且跨平台的开发能力。1)C#是一种类型安全且面向对象的编程语言,旨在与.NET框架无缝集成。2).NET运行时管理C#代码的执行,提供垃圾回收、类型安全等服务,确保高效和跨平台运行。

C#.NET开发:入门的初学者指南C#.NET开发:入门的初学者指南Apr 18, 2025 am 12:17 AM

要开始C#.NET开发,你需要:1.了解C#的基础知识和.NET框架的核心概念;2.掌握变量、数据类型、控制结构、函数和类的基本概念;3.学习C#的高级特性,如LINQ和异步编程;4.熟悉常见错误的调试技巧和性能优化方法。通过这些步骤,你可以逐步深入C#.NET的世界,并编写高效的应用程序。

c#和.net:了解两者之间的关系c#和.net:了解两者之间的关系Apr 17, 2025 am 12:07 AM

C#和.NET的关系是密不可分的,但它们不是一回事。C#是一门编程语言,而.NET是一个开发平台。C#用于编写代码,编译成.NET的中间语言(IL),由.NET运行时(CLR)执行。

c#.net的持续相关性:查看当前用法c#.net的持续相关性:查看当前用法Apr 16, 2025 am 12:07 AM

C#.NET依然重要,因为它提供了强大的工具和库,支持多种应用开发。1)C#结合.NET框架,使开发高效便捷。2)C#的类型安全和垃圾回收机制增强了其优势。3).NET提供跨平台运行环境和丰富的API,提升了开发灵活性。

从网络到桌面:C#.NET的多功能性从网络到桌面:C#.NET的多功能性Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器