1、 查询Student表中的所有记录的Sname、Ssex和Class列。(select sname,ssex,class from student)
Students.Select(s=> new { sname=>s.sname,ssex=>s.ssex, class=>s.class})
linq:from s in Students select new{s.sname, }

2.查询教师所有的单位即不重复的Depart列。select distinct depart from teacher
teachers.distinct().Select(s=>s.depart)
linq: from s in teachers.Distinct() select s.depart

3、 查询Student表的所有记录。select * from Student
student.Select(s=>s)
linq: from s in student select s;

4、 查询Score表中成绩在60到80之间的所有记录。 select * from score where grages bewteen 60 and 80
score.Where(s=>(s.grages>60 && s.grages<80) );
linq: from s in score where s.grages>60 && s.grages<80 select s;

5、 查询Score表中成绩为85,86或88的记录。select * from score where grages in (85,86,88)
linq: from s in Score Where ( new decimal[] {85,86,88}.Contains(s.grage))
Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.grage))

6、 查询Student表中"95031"班或性别为"女"的同学记录。select * from student where class ='95031' or ssex= '女'
student.Where(s=> (s.class=="95031" || s.sex="女"))
from s in student where s.class=="" || s.sex=="" select s;

7、 以Class降序查询Student表的所有记录。 select * from student order by Class DESC
student.OrderByDescing(s=> s.Class)

from s in student order by s.class descending select s;

8、 以Cno升序、Degree降序查询Score表的所有记录。select * from Score order by cno asc and grage desc
score.OrderByDescing(s=>s.grage).OrderBy(s=>s.cno);

from s in score order by s.grage descending order by s.cno ascending select s;

9、 查询"95031"班的学生人数。select count(*) from Students where class='95031'
students.Select(s=>s.class=='95031').Count();
///students.Where(s=>s.class=='').Select(s=>s).Count();

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

11、查询'3-105'号课程的平均分。select avg(grage) from score where cno='3-105'
score.where(s=>s.con=="3-105").Select(s=>s.grage).Avgage();

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5

linq: from s in score where s.con.StartWith("3") group s by s.Cno into cc where cc.count>5
select cc.Avgage(s=>s.grage)

13、查询最低分大于70,最高分小于90的Sno列。select Sno from score group by Sno having min(grage)>70 and
max(grage)<90
linq: from s in score group s by s.sno into cc where cc.min(grage)>70 && cc.Max(grage)<90
select new {sno =>cc.key}

lambda:
scores.GroupBy(s=>s.sno).Where(ss=> ( (ss.Min(cc=>cc.grage)>70) && (ss.Max(cc=>cc.grage)<90 )))
.select(ss=> new {sno=>ss.key})

14、查询所有学生的Sname、Cno和Degree列。
select s.sname,sc.cno,sc.degree from student s,score sc where s.sno = sc.sno

from s in Student join sc in score on s.sno=sc.sno
select new {s.sname,sc.cno, sc.degree}

lambda: Student.join(scores,s=>s.sname,
sc=>sc.cno,
(s,sc)=> new{ sname =s.sname, cno=sc.cno, degree=sc.degree })

//释放资源 .Dispose();
------------------------------------------------------------
.skip(1).Take(4) Skip 上面这段语句的意思是从第二条数据开始显示4条
AppendAndWhere //附加AND查询条件 JoinOnAndWhere //joinon筛选条

Any(): 判断集合中是否有元素满足某一条件
仅返回没有订单的客户:var q =from c in db.Customers where !c.Orders.Any() select c;

All(): 判断集合中所有元素是否都满足某一条件。
var q =from c in db.Customers where c.Orders.All(o => o.ShipCity == c.City) select c;
语句描述:这个例子返回所有订单都运往其所在城市的客户或未下订单的客户。

Contains() :用于判断集合中是否包含有某一元素

Concat(连接): 连接不同的集合,不会自动过滤相同项
var q = (from c in db.Customers select c.Phone).Concat(from c in db.Customers select c.Fax).Concat(from e in db.Employees select e.HomePhone);
语句描述:返回所有消费者和雇员的电话和传真。

Union ::连接不同的集合,自动过滤相同项;延迟。即是将两个集合进行合并操作,过滤相同的项。

Intersect(相交) :取相交项;延迟。即是获取不同集合的相同项(交集)。
Except (与非) :排除相交项;
.Implicit(隐式) Explicit(显式)

ToArray:将序列转换为数组

ToList:将序列转换为泛型列表
-------------------------------------------------------------

有关 Lambda && linq练习 有待整理的更多相关文章

  1. C#基础:LINQ 查询函数整理

    1.LINQ 函数   1.1.查询结果过滤 :where() Enumerable.Where() 是LINQ 中使用最多的函数,大多数都要针对集合对象进行过滤,因此Where()在LINQ 的操作 ...

  2. [MVC4]ASP.NET MVC4+EF5(Lambda/Linq)读取数据

    继续上一节初始ASP.NET MVC4,继续深入学习,感受了一下微软的MVC4+EF5(EntityFramework5)框架的强大,能够高效的开发出网站应用开发系统,下面就看一下如何用MVC4+EF ...

  3. 分享一个递归无限级拼接Json的方法---ExtJs的TreePanel和TreeGrid均适用(Ef,Lambda,Linq,IQueryable,List)

    话不多说,先上实体类,如果你不是codefirst,就把它当成数据表结构. 下面是底层BaseDal获取数据的方法  (如果你没有Base类,直接写在你的DAL层和BLL层) 下面是BaseServi ...

  4. [转][MVC4]ASP.NET MVC4+EF5(Lambda/Linq)读取数据

    本文转自:https://blog.csdn.net/dingxiaowei2013/article/details/29405687 继续上一节初始ASP.NET MVC4,继续深入学习,感受了一下 ...

  5. 几种查询方法(lambda Linq Enumerable静态类方式)

    1.需要一个数据源类: using System; using System.Collections.Generic; namespace Linq { public class Student { ...

  6. C# Lambda && Linq

    Lambda表达式在C#3.0加入,它是一个匿名函数,可用于创建委托或者表达式树类型,运算符为=>,读作"goes to",=>左侧是变量,右侧是表达式,变量类型可以自 ...

  7. 拼linq 时网上整理的一个类

    public static class DynamicLinqExpressions { public static Expression<Func<T, bool>> Tru ...

  8. Lambda表达式使用方法整理

    匿名内部类                                                                               Lambda表达式 匿名内部类  ...

  9. MySQL初步笔记,有待整理

     查询表纪录: select * from tb1; 插入一条记录 insert tb1 values(value1,value2,...); 修改表的默认编码: alter table tb1 ch ...

随机推荐

  1. 7.第一次使用java连接mongodb遇到的问题

    转自:https://blog.csdn.net/u010523770/article/details/54585883 新版本的mongodb的驱动包是依赖bson.jar和mongodb_driv ...

  2. Codefroces 849 A,B

    A. Odds and Ends time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. Fedora27 源配置

    一.添加阿里源,阿里源我感觉是现在国内比较好用的源,支持的发行版比较全.配置方法1.备份系统自带的源mv /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/f ...

  4. 【Uva 1627】Team them up!

    [Link]: [Description] 给你n个人; 有一些人之间有认识关系 a认识b,b不一定认识a 让你把这n个人分成两组 使得这两组中的每一组: 组内的人与人之间都相互认识. 并且,使得两组 ...

  5. Python3爬虫之爬取某一路径的所有html文件

    要离线下载易百教程网站中的所有关于Python的教程,需要将Python教程的首页作为种子url:http://www.yiibai.com/python/,然后按照广度优先(广度优先,使用队列:深度 ...

  6. BZOJ——T 3732: Network

    http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: ...

  7. 用MediaRecorder实现简单的录像功能

    思路:定义一个SurfaceView用来显示预览,在SurfaceHolder的回调中用Camera对象启动预览.然后调用MediaRecorder来录像.仅仅是实现了简单的录像開始和停止功能.顶部能 ...

  8. python-实现xml字符串替换功能

    今天遇到一个问题,说的是要把一个android res目录下,所有name=xx的字符串的值,自己参照网上的方法,写了一个脚本.记录如下,方便以后使用 #!/usr/bin/python # -*- ...

  9. es6 -- Iterator 和 for...of 循环

    1:Iterator(遍历器)的概念 JavaScript 原有的表示“集合”的数据结构,主要是数组(Array)和对象(Object),ES6 又添加了Map和Set.这样就有了四种数据集合,用户还 ...

  10. dfs算法中求数列的组合

    /* 从13个书中挑选5个值,他们的组合可能是 什么, 如下代码 dfs深度遍历, 和全排列是一种方法,但是思路不同 */ public class Main { static int count = ...