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 ...
随机推荐
- js 元素Dom新建并插入页面createElement
纯js var o = document.createElement('script'); o.type = 'text/template'; o.id = 'demo'; document.docu ...
- CSS 温故而知新 断句失败
设置了一定的宽度和高度.但无论是下面哪句都无效. word-break: break-word; word-wrap: break-word; 原因竟然是因为 /* white-space: nowr ...
- cache和内存
CPU与内存 北桥:主桥,主要用来处理高速信号,负责与处理器的联系:CPU通过FSB前端总线来访问内存控制器. 南桥:IO桥,负责IO总线之间的通信,比如PCI总线.SATA.USB等,可以连接光驱. ...
- spring download
http://maven.springframework.org/release/org/springframework/spring/
- grep -rl tttt /data/ 命令在 /data 目录下面搜寻包含tttt字符的命令
grep --help -R, -r, --recursive equivalent to --directories=recurse -l, --files-with-matches print o ...
- 【一键激活win8.1系统】
下载激活工具地址: 链接:https://pan.baidu.com/s/1AUaQQRcqfCYWK94KapYjjA 密码:i4sk 下载后,右键单击“以管理员身份运行”Microsoft Too ...
- Objective-C 的动态提示和技巧
过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语言转过来的,如Java或C#,当然也有直接以Objective-C作为入门语 ...
- 一种关键字搜索---edu.cn
比如要搜索知识点最小二乘,可以这样: 曲线拟合的最小二乘法 edu.cn 然后就一大片关于edu的相关链接,很多知名学校链接 http://www.bb.ustc.edu.cn/jpkc/xiaoji ...
- WPF使用X:Static做多语言支持
让程序支持多语言,一般把需要显示的字符串保存在一个资源类的static属性中. <!--[if !supportLists]--> <!--[endif]--> 微软的WPF程 ...
- java基础之【堆、栈、方法区】结构图
|--数组实例化过程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHViaWFvXzA2MTg=/font/5a6L5L2T/fontsize/400/ ...