zoukankan      html  css  js  c++  java
  • SQL、Linq和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中被广泛使用。

    Lambda:Lambda是表达式,Linq是语句!表达式要符号链接,语句就不用了!表达式是类似C#语法的写法,语句是类数据库语法的写法!

    对比实现

    ※查询全部内容

    1 查询Student表的所有记录。 select * from student

    2 Linq: from s in Students 5 select s

    3Lambda: 7 Students.Select( s => s)

    ※按列查询

    select sname,ssex,class from student

    Linq: from s in stuents 

       select new {

        s.SNAME,

        Vs.SSEX,

        s.CLASS

      }

    Lambda:Students.Select( s => new {SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS})

    ※distinct去重查询

    查询教师所有的单位即不重复的Depart列。

    select distinct depart from teacher

    Linq: from t in Teachers.Distinct()select t.DEPART

    Lambda: Teachers.Distinct().Select( t => t.DEPART) 

    ※两个区间内查询

    查询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))

    ※在一个范围内查询

    select * from score where degree in (85,86,88)

    Linq: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))

    ※或关系查询

    查询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 == "女"))

    ※排序

    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)

    ※行数查询

    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()

     

    ※平均值查询

    查询'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)

    ※嵌套查询

    查询Score表中的最高分的学生学号和课程号。

     

    传送门:http://blog.csdn.net/u010926964/article/details/46240215

  • 相关阅读:
    前端UI框架
    Knowledge
    Microsoft SQL Server
    ASP.NET MVC
    将博客搬至CSDN
    python中的数据类型
    python基础知识
    接口和抽象类的区别
    面向对象的四大特征
    数据结构学习笔记
  • 原文地址:https://www.cnblogs.com/liubaojing/p/8621438.html
Copyright © 2011-2022 走看看