SQL   LinqToSql   Lambda

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 }

SQL、LINQ、Lambda 三种用法的更多相关文章

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

    SQL.LINQ.Lambda 三种用法颜色注释: SQL LinqToSql Lambda QA1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname, ...

  2. using 的三种用法

    using 有哪三种用法? 1)引入命名空间. 2)给命名空间或者类型起别名. 3)划定作用域.自动释放资源,使用该方法的类型必须实现了 System.IDisposable接口,当对象脱离作用域之后 ...

  3. Js闭包常见三种用法

        Js闭包特性源于内部函数可以将外部函数的活动对象保存在自己的作用域链上,所以使内部函数的可以将外部函数的活动对象占为己有,可以在外部函数销毁时依然存有外部函数内的活动对象内容,这样做的好处是可 ...

  4. SQL,LINQ,Lambda语法对照图(转载)

    如果你熟悉SQL语句,当使用LINQ时,会有似曾相识的感觉.但又略有不同.下面是SQL和LINQ,Lambda语法对照图 SQL LINQ Lambda SELECT * FROM HumanReso ...

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

    前几天去家公司面试,有一道这样的题:写出c#中new关键字的三种用法,思前想后挖空心思也只想出了两种用法,回来查了下msdn,还真是有第三种用法:用于在泛型声明中约束可能用作类型参数的参数的类型,这是 ...

  6. c# new关键字的三种用法

    三种用法如下: 在 C# 中,new 关键字可用作运算符.修饰符或约束. 1)new 运算符:用于创建对象和调用构造函数. 2)new 修饰符:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成 ...

  7. java中 this 的三种用法

    Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...

  8. 子查询。ANY三种用法。ALL两种用法。HAVING中使用子查询。SELECT中使用子查询。

    子查询存在的意义是解决多表查询带来的性能问题. 子查询返回单行多列: ANY三种用法: ALL两种用法: HAVING中的子查询返回单行单列: SELECT中使用子查询:(了解就好,避免使用这种方法! ...

  9. java中 this 关键字的三种用法

    Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...

随机推荐

  1. python del函数

    del用于list列表及字典操作,删除一个或者连续几个元素也可以删除字典指定的key   示例程序如下: >>> a = [-1, 3, 'aa', 85] # 定义一个list&g ...

  2. Linux多进行之fork

    #include <unistd.h> //定义该函数 #include <sys/types.h> //定义函数的返回类型pid_t /* 功能:复制进程 参数:无 返回值: ...

  3. vi使用教程

    Vi有3种模式: 命令模式——命令操作 插入模式——进入vi之后,输入i/a/o,按Esc键,进入命令模式 编辑模式——:set nu, 以回车结束 1.插入 a - 光标后插入 A - 本行末尾插入 ...

  4. ENVI中利用polygon掩膜修改类到指定类

    overlay——classification——制定分类的图像 edit——polygon delete from class(选择这个掩膜模式) edit——set delete class va ...

  5. (转)《深入理解java虚拟机》学习笔记3——垃圾回收算法

    Java虚拟机的内存区域中,程序计数器.虚拟机栈和本地方法栈三个区域是线程私有的,随线程生而生,随线程灭而灭:栈中的栈帧随着方法的进入和退出而进行入栈和出栈操作,每个栈帧中分配多少内存基本上是在类结构 ...

  6. wampserver安装后的基本配置

    wampserver安装后的基本配置 1.WampServer的安装 下载好安装包后,你能在保存其文件夹中找到这样一个图标: 双击它,会弹出如下提示 提示信息:不要试图从Wamp5 1.x(x代表任意 ...

  7. HttpClient抓取网页内容简单介绍

    版本HttpClient3.1 1.GET方式 第一步.创建一个客户端,类似于你用浏览器打开一个网页 HttpClient httpClient = new HttpClient(); 第二步.创建一 ...

  8. MITK-Qt4.8.4(x64)+VS2012+Win7_X64 编译记录

    本文参考 http://blog.csdn.net/lanxuxml/article/details/9232529(中文) http://docs.mitk.org/nightly-qt4/Buil ...

  9. asp.net web api 开发时应当注意的事项

    Self referencing when returning chain of objects. This can be solved using a design pattern called t ...

  10. 2001: [Hnoi2010]City 城市建设 - BZOJ

    DescriptionPS国是一个拥有诸多城市的大国,国王Louis为城市的交通建设可谓绞尽脑汁.Louis可以在某些城市之间修建道路,在不同的城市之间修建道路需要不同的花费.Louis希望建造最少的 ...