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 To SQL && Lambda 使用方法小结 (转)的更多相关文章

  1. 也记一次性能优化:LINQ to SQL中Contains方法的优化

    距离上一篇博文更新已经两个月过去了.在此,先表一表这两个月干了些啥: 世界那么大,我也想去看看.四月份的时候,我入职了上海的一家电商公司,职位是.NET高级开发工程师.工作一个月,最大的感受是比以前小 ...

  2. SQL server 分页方法小结

    这里面介绍一下常用的分页方法: 1.使用top来分页 select top @pageSize * from table where id not in (select top @pageSize*( ...

  3. linq自定义条件Lambda过滤方法

    Public Func<NoramalClass,bool>simpleComare<NormalClass>(string property,object value) { ...

  4. 年终巨献 史上最全 ——LINQ to SQL语句

    LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...

  5. LINQ TO SQL 大全

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 LINQ to SQL语句(1)之Where 适用场景: ...

  6. LINQ to SQL大全

    LINQ to SQL语句 (1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的 ...

  7. [转]LINQ To SQL 语法及实例大全

    转载自:http://blog.csdn.net/pan_junbiao/article/details/7015633 LINQ to SQL语句(1)之Where Where操作 适用场景:实现过 ...

  8. LINQ to SQL语句非常详细(原文来自于网络)

    LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...

  9. LINQ To SQL 语法及实例大全

    http://blog.csdn.net/pan_junbiao/article/details/7015633 http://blog.csdn.net/pan_junbiao/article/de ...

随机推荐

  1. c语言-猜数字游戏

    #include <stdio.h> #include <stdlib.h> int top(); int input(); void main() { ; int numbe ...

  2. 开启真机的View Server引入HierarchyViewer/By写monkeyrunner自动化测试脚本

    其实相关文章网上也有不少了,不过在真机上开启View Server的中文文章好像只有一篇,前段时间按照这篇文章的内容,并结合英文源文去hack我的Nexus S(4.1.2)也走了一点弯路.现在总结一 ...

  3. NetBox v2.8下载使用指南

    2008-09-20 11:21:05|  分类: ASP|举报|字号 订阅     NetBox v2.8下载地址: http://down.chinaz.com/soft/13211.htm 安装 ...

  4. Java线程:新特征-有返回值的线程

    http://lavasoft.blog.51cto.com/62575/222082/ Java线程:新特征-有返回值的线程 2009-11-04 17:33:56 标签:返回值 职场 线程 休闲 ...

  5. Hadoop中的一些基本操作

    先粗略说一下“hadoop fs”和“hadoop dfs”的区别:fs是各比较抽象的层面,在分布式环境中,fs就是dfs,但在本地环境中,fs是local file system,这个时候dfs不可 ...

  6. [转]Zend Studio 文件头和方法注释设置

    在zend studio ide 7.1 中选择窗口->首选项->PHP–>编辑器 –>模板 –>新建然后添加 funinfo或fileinfo 模板代码根据下边定义的C ...

  7. QQ 自动接收远程连接之关闭了远程桌面

    之前使用都好好的,后来就不知道怎么了突然就不行了,在另外一个远程桌面软件(向日葵)失效后,木有办法,查查查,终于查出来了,是我本机的时间服务停止了,导致我本机的时间和服务器时间不一致,所以连接不上.只 ...

  8. mysql if then

    CREATE PROCEDURE userinfo_modify( IN id INT ,IN loginid INT ,IN levelid INT ,IN namestr VARCHAR(50) ...

  9. mysql innobackupex备份工具

    先简单介绍一下这个工具:innobackupexinnobackupex比xtarbackup有更强的功能,它整合了xtrabackup和其他的一些功能,他不但可以全量备份/恢复,还可以基于时间的增量 ...

  10. Java学习笔记之基于TCP协议的socket

    可以一直输入,而不是一问一答: 开两个线程,一个负责收,一个负责发. 1.先运行: package com.zr.javase0825; import java.io.BufferedReader; ...