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 }

 

Linq练习题的更多相关文章

  1. 1.解剖Linq to object

    LINQ想必大家都不陌生了,它的出现使得我们的代码变得更短.更优雅了.至于LINQ是什么,Linq to object这类的扩展方法到底做了些什么.我们使用的EF是如何实现的(如何解析Expressi ...

  2. 练习题:试使用C#编程实现银行、ATM等功能

    练习题:试使用编程实现银行.ATM等功能 using System; using System.Collections.Generic; using System.Linq; using System ...

  3. Linq表达式、Lambda表达式你更喜欢哪个?

    什么是Linq表达式?什么是Lambda表达式? 如图: 由此可见Linq表达式和Lambda表达式并没有什么可比性. 那与Lambda表达式相关的整条语句称作什么呢?在微软并没有给出官方的命名,在& ...

  4. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  5. [C#] 走进 LINQ 的世界

    走进 LINQ 的世界 序 在此之前曾发表过三篇关于 LINQ 的随笔: 进阶:<LINQ 标准查询操作概述>(强烈推荐) 技巧:<Linq To Objects - 如何操作字符串 ...

  6. [C#] 进阶 - LINQ 标准查询操作概述

    LINQ 标准查询操作概述 序 “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法.大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T> ...

  7. LINQ to SQL语句(7)之Exists/In/Any/All/Contains

    适用场景:用于判断集合中元素,进一步缩小范围. Any 说明:用于判断集合中是否有元素满足某一条件:不延迟.(若条件为空,则集合只要不为空就返回True,否则为False).有2种形式,分别为简单形式 ...

  8. .NET深入实战系列—Linq to Sql进阶

    最近在写代码的过程中用到了Linq查询,在查找资料的过程中发现网上的资料千奇百怪,于是自己整理了一些关于Linq中容易让人困惑的地方. 本文全部代码基于:UserInfo与Class两个表,其中Cla ...

  9. LINQ Group By操作

    在上篇文章 .NET应用程序与数据库交互的若干问题 这篇文章中,讨论了一个计算热门商圈的问题,现在在这里扩展一下,假设我们需要从两张表中统计出热门商圈,这两张表内容如下: 上表是所有政区,商圈中的餐饮 ...

随机推荐

  1. opencv3计算机视觉+Python(一)

    基本I/O脚本 读/写图像文件 OpenCV的imread函数和imwrite函数能支持各种静态图像文件格式.不同系统支持的文件格式不一样,但都支持BMP格式,通常还应该支持PNG.JPEG和TIFF ...

  2. sublime使用心得

    1.ctrl + shift +p 命令面板 ---> toggle_side_bar 2.ctrl + shift +p 命令面板 --->reindent lines 3.ctrl + ...

  3. Oracle 11G无法导出空表的解决办法

    11G中有个新特性,当表无数据时,不分配segment,以节省空间解决方法:1.insert一行,再rollback就产生segment了.该方法是在在空表中插入数据,再删除,则产生segment.导 ...

  4. Groovy系列-groovy比起Java--有哪些地方写起来更舒服?

    groovy比起java-有哪些地方写起来更舒服 java发展缓慢,语法落后冗余 说起java,其实java挺好的,java现在的性能也不错,但是,java的语法显然比较落后,而且冗余,getter/ ...

  5. 0608pm单例模式and面向对象的六大原则

    //把类控制住,不让外界造她的对象class DA{ public $name; static private $dx;//存放对象的变量 //将构造变为私有,外界没法造对象 private func ...

  6. 爬虫学习笔记(2)--创建scrapy项目&&css选择器

    一.手动创建scrapy项目---------------- 安装scrapy: pip install -i https://pypi.douban.com/simple/  scrapy    1 ...

  7. loadrunder之脚本篇——定义全局变量

    如果参数是全局的,在脚本中的任何一个Action中都可以使用,变量一般是局部的,如果跨Action调用会出现未声明的错误. 打开Script视图中左侧Action列表中的globals.h文件,可定义 ...

  8. linux 基础二---用户群租权限

    用户&群组&权限 一.用户 1.用户及passwd文件 1) 掌握/etc/passwd文件的功能:存储所有用户的相关信息,该文件也被称为用户信息数据库(Database). 2) / ...

  9. Windows下MarialDB使用

    命令行控制启动和关闭:mysqld --console     #这样启动ctrl+c即为关闭 启动:双击mysqld.exe即可   #此为后台启动 关闭:mysqladmin -uroot -pr ...

  10. nodejs安装,配置环境,使用express建立一个新项目

    1.下载nodejs安装包 去nodejs官网下载最新版本就行,网址:http://nodejs.cn/download/,点击自己适用的系统,自动下载跟电脑操作系统位数符合的安装包, 下载下来安装包 ...