LINQ 学习路程 -- 查询操作 where
1.where
| Filtering Operators | Description |
|---|---|
| Where | Returns values from the collection based on a predicate function |
| OfType | Returns values from the collection based on a specified type. However, it will depend on their ability to cast to a specified type. |
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
Func<TSource, bool> predicate); public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate);
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
};
var filteredResult = from s in studentList
where s.Age > && s.Age <
select s.StudentName;
方式2
Func<Student,bool> isTeenAger = delegate(Student s) {
return s.Age > && s.Age < ;
};
var filteredResult = from s in studentList
where isTeenAger(s)
select s;
方式3
public static void Main()
{
var filteredResult = from s in studentList
where isTeenAger(s)
select s;
} public static bool IsTeenAger(Student stud)
{
return stud.Age > && stud.Age < ;
}
where的第二个扩展方法包含集合的index索引
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Steve", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
};
var filteredResult = studentList.Where((s, i) => {
if(i % == ) // if it is even element
return true;
return false;
});
foreach (var std in filteredResult)
Console.WriteLine(std.StudentName);
多个where从句
var filteredResult = from s in studentList
where s.Age >
where s.Age <
select s;
var filteredResult = studentList.Where(s => s.Age > ).Where(s => s.Age < );
需要记住的几点:
1.Where根据特定条件来筛选集合元素
2.where扩展方法有2个重载,使用第二个重载方法可以知道当前元素在集合中的索引位置
3.方法语法需要整个lambda表达式,而查询语法仅仅需要表达式主体
4.在单一的LINQ查询中可以使用多个where从句
LINQ 学习路程 -- 查询操作 where的更多相关文章
- LINQ 学习路程 -- 查询操作 Expression Tree
表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...
- LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending
Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...
- LINQ 学习路程 -- 查询操作 Join
Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...
- LINQ 学习路程 -- 查询操作 GroupBy ToLookUp
Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...
- LINQ 学习路程 -- 查询操作 let into关键字
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
- LINQ 学习路程 -- 查询操作 Aggregate
聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...
- LINQ 学习路程 -- 查询操作 Select, SelectMany
IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...
- LINQ 学习路程 -- 查询操作 OfType
OfType操作根据集合中的元素是否是给定的类型进行筛选 IList mixedList = new ArrayList(); mixedList.Add(); mixedList.Add(" ...
随机推荐
- $.ajax 温故而知新坑
$.ajax的配置项中使用 contentType: "application/json" 时,Data选项允许为String类型,如JSON.stringify({abc:123 ...
- 编程算法 - 求1+2+...+n(构造函数) 代码(C++)
求1+2+...+n(构造函数) 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 求1+2+...+n, 要求不能使用乘除法\for\whi ...
- vs 2015 update 3各版本下载地址
微软在06月27日发布了Visual Studio 2015 Update 3 .在MSDN中微软也提供下载,而且MSDN的Visual Studio 2015 Update 3与官方免费下载的文件是 ...
- 2017湘潭赛 A题 Determinant (高斯消元取模)
链接 http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1260 今年湘潭的A题 题意不难 大意是把n*(n+1)矩阵去掉某一列 ...
- SQL索引及表的页的逻辑顺序与物理顺序
1.经过测试发现当聚集索引新建或者重建时,会按照逻辑顺序重新排列数据页和数据页内的数据行的物理顺序. 2.但修改表时,无论是聚集索引还是堆的数据页都是按自然顺序向后插入数据,页面上的偏移量可以证明.因 ...
- java实体类如果不重写toString方法,会如何?
先认识一下Object Object 类的 toString 方法 返回一个字符串,该字符串由类名(对象是该类的一个实例).at 标记符“@”和此对象哈希码的无符号十六进制表示组成.换句话说,该方法返 ...
- Huffman编码(Huffman树)
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "Huffman编码(Huffman树)" 的idea 并用源代码加以实现: 0.2) ...
- node.js实现国标GB28181设备接入的sip服务器解决方案
方案背景 在介绍GB28181接入服务器的方案前,咱们先大概给大家介绍一下为什么我们选择了用nodejs开发国标GB28181的服务,我大概给很多人介绍过这个方案,大部分都为之虎躯一震,nodejs在 ...
- zabbix server is not running解决办法
正常安装完zabbix后,登录后zabbix监控报错zabbix server is not running: the information displayed may not be current ...
- python函数的作用域
python函数的作用域 以下内容参考自runoob网站,以总结python函数知识点,巩固基础知识,特此鸣谢! 原文地址:http://www.runoob.com/python3/python3- ...