LINQ 学习路程 -- 查询例子
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 学习路程 -- 查询例子的更多相关文章
- LINQ 学习路程 -- 查询操作 let into关键字
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...
- LINQ 学习路程 -- 查询操作 Expression Tree
表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...
- LINQ 学习路程 -- 查询操作 Join
Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...
- LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending
Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...
- LINQ 学习路程 -- 查询操作 where
1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...
- LINQ 学习路程 -- 查询语法 LINQ Query Syntax
1.查询语法 Query Syntax: from <range variable> in <IEnumerable<T> or IQueryable<T> ...
- LINQ 学习路程 -- 查询操作 Select, SelectMany
IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...
- LINQ 学习路程 -- 查询操作 GroupBy ToLookUp
Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...
随机推荐
- JQuery小结(转)
一.页面加载 JQ的页面加载比JS要快,当整个dom树结构生成完毕后就会加载 JQ页面加载不存在覆盖问题,加载的时候是顺序执行 JQ的页面加载最简写的方式为: $(function(){ alert( ...
- bean对应mapper.xml字段
在bean中set的时候最好写上这个,避免报空指针............... public void setImgAddress(String imgAddress) { this.imgAddr ...
- ArrayList add remove 代码分析
Add 首次add 元素需要对数组进行扩容(初始化Capacity 10, 第二次扩容10>>1 为5, 第三次扩容15>>1 为7), 每次扩容之前长度的1.5倍,当add ...
- [译]GLUT教程 - 位图和正交投影视图
Lighthouse3d.com >> GLUT Tutorial >> Fonts >> Bitmap Fonts and Orthogonal Projecti ...
- 使用SpannableString实现一个load小动画
依然是github开源项目:WaitingDots 这个项目代码不多,实现的非常easy.可是非常有意思由于动画的基本元素不是画出来的,而是使用了spannableString来实现. DotsTex ...
- struts2中配置全局日期类型转换器
1.编写一个类,继承StrutsTypeConverter,实现其中的convertFromString和convertToString方法,该类如下: package me.edu.utils; i ...
- 【JMeter4.0学习(六)】之逻辑控制器说明
主要demo例子在: 链接: https://pan.baidu.com/s/1OFdsrNG7PTOYQ8TdjiVtBQ 密码: tkd2 汇总参考文档:<Jmeter之逻辑控制器(Logi ...
- postman 测试API - 获取/调用 token
1.使用全局变量保存token 2.再调用 参考文章 http://www.jianshu.com/p/13c8017bb5c8 https://testerhome.com/topics/6555
- Fiddler 抓取 ios 端数据包
前提条件: 1. Fiddler 工具安装完成,并授权成功,可以完成网页的http 协议拦截. 2. iphone X 一部 ☺ 3. 360wifi 一个[同一局域网内,任何wifi都可以设置,其他 ...
- lua 元表是个啥?
function readOnly(t) local proxy = {} local mt = { __index = t, __newindex = function(t,k,v) error(& ...