LINQ 学习路程 -- 查询操作 where
2024-09-06 12:04:00
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从句
最新文章
- 透过WinDBG的视角看String
- 优秀网站看前端 —— 小米Note介绍页面
- Magento代码之订单创建流程
- MyBatis学习(一)
- java文件读写操作大全
- BI的相关问题[转]
- C++的异常处理之一:throw是个一无是处的东西
- jquery常用正则表达式
- A+B Problem 详细解答 (转载)
- [fedora21]给fedora21安装fcitx输入法
- struts2 文件的上传下载 表单的重复提交 自定义拦截器
- 免费的天气预报API--谷歌,雅虎,中央气象台
- [JAVA]HDU 4919 Exclusive or
- 关于点击空白关闭弹窗的js写法推荐?
- iOS tableView的图片缓存异步载入
- 显示器中关于HS,VS,HBP,VBP参数浅析
- scss语法介绍
- xadmin快速搭建后台管理系统
- 如何用java语言实现C#中的ref关键字(按引用传递参数)的效果
- SSH命令行管理文件
热门文章
- java 安装后 不能 java javac 说找不到命令 -bash: javac: command not found
- linux LVS (keepalived+ipvsadm)负载均衡搭建
- webuploader插件使用中的一点东西
- Java并发编程(一)学习大纲
- Project Euler:Problem 87 Prime power triples
- NPTL LinuxThreads
- zabbix自动化监控三种方式
- 蒙特卡洛方法计算圆周率的三种实现-MPI openmp pthread
- php 在linux 用file_exists() 函数判断 另外一台服务器映射过来的文件是否存在 总是返回false
- Linux5_环境变量