IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = , StandardID = } ,
new Student() { StudentID = , StudentName = "Steve", Age = , StandardID = } ,
new Student() { StudentID = , StudentName = "Bill", Age = , StandardID = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = , StandardID = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; IList<Standard> standardList = new List<Standard>() {
new Standard(){ StandardID = , StandardName="Standard 1"},
new Standard(){ StandardID = , StandardName="Standard 2"},
new Standard(){ StandardID = , StandardName="Standard 3"}
};

1.多个select和where操作

var studentNames = studentList.Where(s => s.Age > )
.Select(s => s)
.Where(st => st.StandardID > )
.Select(s => s.StudentName);
var teenStudentsName = from s in studentList
where s.age > && s.age <
select new { StudentName = s.StudentName }; teenStudentsName.ToList().ForEach(s => Console.WriteLine(s.StudentName));

2.Group by

var studentsGroupByStandard = from s in studentList
group s by s.StandardID into sg
orderby sg.Key
select new { sg.Key, sg }; foreach (var group in studentsGroupByStandard)
{
Console.WriteLine("StandardID {0}:", group.Key); group.sg.ToList().ForEach(st => Console.WriteLine(st.StudentName ));
}

left outer join

var studentsGroup = from stad in standardList
join s in studentList
on stad.StandardID equals s.StandardID
into sg
select new {
StandardName = stad.StandardName,
Students = sg
}; foreach (var group in studentsGroup)
{
Console.WriteLine(group.StandardName); group.Students.ToList().ForEach(st => Console.WriteLine(st.StudentName));
}
var studentsWithStandard = from stad in standardList
join s in studentList
on stad.StandardID equals s.StandardID
into sg
from std_grp in sg
orderby stad.StandardName, std_grp.StudentName
select new {
StudentName = std_grp.StudentName,
StandardName = stad.StandardName
}; foreach (var group in studentsWithStandard)
{
Console.WriteLine("{0} is in {1}", group.StudentName, group.StandardName);
}

Sorting

var sortedStudents = from s in studentList
orderby s.StandardID, s.age
select new {
StudentName = s.StudentName,
Age = s.age,
StandardID = s.StandardID }; sortedStudents.ToList().ForEach(s => Console.WriteLine("Student Name: {0}, Age: {1}, StandardID: {2}", s.StudentName, s.Age , s.StandardID));

inner join

var studentWithStandard = from s in studentList
join stad in standardList
on s.StandardID equals stad.StandardID
select new {
StudentName = s.StudentName,
StandardName = stad.StandardName
};
var nestedQueries = from s in studentList
where s.age > && s.StandardID ==
(from std in standardList
where std.StandardName == "Standard 1"
select std.StandardID).FirstOrDefault()
select s; nestedQueries.ToList().ForEach(s => Console.WriteLine(s.StudentName));

LINQ 学习路程 -- 查询例子的更多相关文章

  1. LINQ 学习路程 -- 查询操作 let into关键字

    IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...

  2. LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行

    延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...

  3. LINQ 学习路程 -- 查询操作 Expression Tree

    表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...

  4. LINQ 学习路程 -- 查询操作 Join

    Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...

  5. LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending

    Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...

  6. LINQ 学习路程 -- 查询操作 where

    1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...

  7. LINQ 学习路程 -- 查询语法 LINQ Query Syntax

    1.查询语法 Query Syntax: from <range variable> in <IEnumerable<T> or IQueryable<T> ...

  8. LINQ 学习路程 -- 查询操作 Select, SelectMany

    IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...

  9. LINQ 学习路程 -- 查询操作 GroupBy ToLookUp

    Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...

随机推荐

  1. NodeJS示例异步式(Asynchronous)IO与同步式Synchronous)IO

    理解IO      IO(Input/Output)通常是指计算机线程进行慈磁盘读写或者网络通信时的一种行为.   同步式(Synchronous)IO和异步式(Asynchronous )IO   ...

  2. 千万级的大表!MySQL这样优化更好

    对于一个千万级的大表,现在可能更多的是亿级数据量,很多人第一反应是各种切分,可结果总是事半功倍,或许正是我们优化顺序的不正确.下面我们来谈谈怎样的优化顺序可以让效果更好. MySQL数据库一般都是按照 ...

  3. Cassandra安装和初次使用

    Cassandra安装和初次使用 卡珊德拉(Cassandra)又译卡桑德拉.卡珊卓,为希腊.罗马神话中特洛伊(Troy)的公主,阿波罗(Apollo)的祭司.因神蛇以舌为她洗耳或阿波罗的赐予而有预言 ...

  4. Y2K Accounting Bug - 2586

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11271   Accepted: 5672 Description Ac ...

  5. CentOS 7 mini 试用笔记

    CentOS 7 mini 试用笔记 安装过程中,网卡是默认关闭的,要手动打开. 安装好以后,查看IP地址:# ip addr----------------------1: lo: 本地回环2: e ...

  6. html5小趣味知识点系列(一)spellcheck

    发现一些h5的系ode知识点增加一些趣味性 实用性 不敢妄自评论  觉得有用就用一下  没用就路过一下 spellcheck属性 它的功能是针对用户输入的文本内容进行拼写和语法检查 用于input 和 ...

  7. 源码安装Apache,报错:Cannot use an external APR with the bundled APR-util

    一般在第一次源码安装是没有问题的,在版本变化情况下在次源码安装可能会遇到此问题: apache2.0.x与apache2.2.x在apr有很大区别,前者为依赖公用apr,后者依赖于自身的apr.一般前 ...

  8. 在Linux中显示日历(cal)

    cal 2013    显示2013年整年日历 cal 7 2013  显示2013年 7 月 日历

  9. java开发知识体系

  10. 【BZOJ3926】[Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机

    [BZOJ3926][Zjoi2015]诸神眷顾的幻想乡 Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝 ...