http://kb.cnblogs.com/page/73528/

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 }

主要参考文章链接:http://www.cnblogs.com/RuiLei/archive/2008/11/09/1329905.html

通过16道练习学习Linq和Lambda的更多相关文章

  1. 过16道练习学习Linq和Lambda(转)

    1. 查询Student表中的所有记录的Sname.Ssex和Class列. select sname,ssex,class from studentLinq: from s in Students ...

  2. LINQ使用Lambda表达式选择几列

    学习LINQ的Lambda的表达式,尝试从数据集合中,选择其中几列. 创建一个model: source code: namespace Insus.NET.Models { public class ...

  3. 学习LINQ必备条件

    转自:http://www.cnblogs.com/VolcanoCloud/p/4451302.html 学习LINQ必备条件:隐式类型局部变量:对象集合初始化器:委托:匿名函数:lambda表达式 ...

  4. 用linqPad帮助你快速学习LINQ

    在这里我向大家推荐的一个具是LinqPad有了这个工具并熟练使用就可以很快学习并掌握linq linqPad下载地址:http://www.linqpad.net/ 它也自带了很多例子方便大家查询,l ...

  5. linqPad快速学习LINQ(含视频)

    在这里我向大家推荐的一个具是LinqPad有了这个工具并熟练使用就可以很快学习并掌握linq 安装步骤: 使用LINQPad可以很方便的调试linq以及lambda表达式.其中自带了linq以及F#简 ...

  6. 白话LINQ系列2---以代码演进方式学习LINQ必备条件

    今天,我们通过一个简单的示例代码的演进过程,来学习LINQ必备条件:隐式类型局部变量:对象集合初始化器:委托:匿名函数:lambda表达式:扩展方法:匿名类型.废话不多说,我们直接进入主题. 一.实现 ...

  7. Linq之Lambda表达式初步认识

    目录 写在前面 匿名方法 一个例子 Lambda 定义 一个例子 总结 参考文章 写在前面 元旦三天在家闲着无事,就看了看Linq的相关内容,也准备系统的学习一下,作为学习Linq的前奏,还是先得说说 ...

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

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

  9. 学习LINQ,发现一个好的工具。LINQPad!!

    今日学习LINQ,发现一个好的工具.LINQPad!! 此工具的好处在于,不需要在程序内执行,直接只用工具测试.然后代码通过即可,速度快,简洁方便. 可以生成其LINQ查询对应的lambda和SQL语 ...

随机推荐

  1. DIOCP数据包太大,请在业务层分拆发送

    DIOCP数据包太大,请在业务层分拆发送 DIOCP日志记录异常:数据包太大,请在业务层分拆发送...... 跟踪发现,原因在下图:

  2. Blocks Programming Topics

    最近的工作中比较频繁的用到了Block,不在是以前当做函数指针的替代或者某些API只有Blocks形式的接口才不得已用之了,发现自己对其了解还是太浅,特别是变量的生存期,按惯例还是翻译官方文档,原文链 ...

  3. 从零开始写一个发送h264的rtsp服务器(上)

    转自:http://blog.csdn.net/jychen105/article/details/47006345 一.什么是RTSP 通常所说的rtsp协议其实包含三个协议: rtsp协议, rt ...

  4. 实习医生格蕾第一季/全集Grey’s Anatomy迅雷下载

    第一季 Grey's Anatomy Season 1 (2005)看点:欢迎来到格雷斯医院,哈佛医学院西区最艰苦的外科住院医实习程序,医务新人最残酷的训练场.Meredith.Izzie和Chris ...

  5. Asp.Net Core MVC控制器和视图之间传值

    一.Core MVC中控制器和视图之间传值方式和Asp.Net中非常类似 1.弱类型数据:ViewData,ViewBag 2.强类型数据:@model 二.代码 实例  1.ViewData pub ...

  6. 标准输出中stderr和stdout的区别

    一.首先介绍一下三者printf,sprintf,fprintf的功能 1,printf就是标准输出,在屏幕上打印出一段字符串来. 2,sprintf就是把格式化的数据写入到某个字符串中.返回值字符串 ...

  7. Java IO流关闭问题的深入研究

    转自:https://blog.csdn.net/maxwell_nc/article/details/49151005 前几天看了一篇文章(见参考文章),自己动手试了下,发现有些不一样结论,作博客记 ...

  8. [leetcode]Partition List @ Python

    原题地址:https://oj.leetcode.com/problems/partition-list/ 题意: Given a linked list and a value x, partiti ...

  9. [leetcode]Anagrams @ Python

    原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of ...

  10. 创建SQL作业错误的解决方法(不能将值 NULL 插入列 'owner_sid',表 'msdb.dbo.sysjobs';列不允许有空值。)

    在用SQL语句创建SQL Server作业时有时出现如下错误: 消息 515,级别 16,状态 2,过程 sp_add_job,第 137 行 不能将值 NULL 插入列 'owner_sid',表  ...