List集合查询数据

List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees.Add(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") });
employees.Add(new Employees { Id = 4, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") });
employees.Add(new Employees { Id = 5, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") });
All<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)确定序列中的所有元素是否都满足条件。
Any<TSource>(IEnumerable<TSource>) 确定序列是否包含任何元素。
//表示集合中的任一个元素满足表达式条件,即返回true。
var res1 = employees.Any(e => e.Id == 3 || e.Id == 8);
Console.WriteLine(res1); //out:True var res2 = employees.Any(e => e.Id == 3 && e.Name == "Steven");
Console.WriteLine(res2); //out:False 数据源Name中没有Steven
Contains(T) 确定某元素是否在 List<T> 中。
Equals(Object) 确定指定的对象是否等于当前对象。(继承自 Object)
Exists(Predicate<T>) 确定 List<T> 是否包含与指定谓词定义的条件匹配的元素。
Find(Predicate<T>) 搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中的第一个匹配元素。
List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees.Add(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") });
employees.Add(new Employees { Id = 4, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") });
employees.Add(new Employees { Id = 5, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") }); //如果找到与指定谓词定义的条件匹配的第一个元素,则为该元素;
var emp = employees.Find(e => e.Id == 5);
Console.WriteLine(emp.Id); //5
Console.WriteLine(emp.Name); //Steven.Buchanan
Console.WriteLine(emp.City); //London
Console.WriteLine(emp.BirthDate); //1955-03-04
FindAll(Predicate<T>) 检索与指定谓词定义的条件匹配的所有元素。
List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees.Add(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") });
employees.Add(new Employees { Id = 4, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") });
employees.Add(new Employees { Id = 5, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") }); //如果找到一个 List<T>,其中所有元素均与指定谓词定义的条件匹配,则为该数组;否则为一个空
var emp = employees.FindAll(e => e.Id >=3);
//遍历访问
foreach (var item in emp)
{
Console.WriteLine(item.Id);
Console.WriteLine(item.Name);
Console.WriteLine(item.City);
Console.WriteLine(item.BirthDate);
}
FindIndex(Predicate<T>)    搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中第一个匹配元素的从零开始的索引。
List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees.Add(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") });
employees.Add(new Employees { Id = 4, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") });
employees.Add(new Employees { Id = 5, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") }); //如果找到与 match 定义的条件相匹配的第一个元素,则为该元素的从零开始的索引;否则为 -1
var emp = employees.FindIndex(e => e.Id ==3);
Console.WriteLine("查到的索引为:" + emp); //2
FindLast(Predicate<T>)    搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中的最后一个匹配元素。
使用方式同FindIndex
IndexOf(T)    搜索指定的对象,并返回整个 List<T> 中第一个匹配项的从零开始的索引
LastIndexOf(T)    搜索指定对象并返回整个 List<T> 中最后一个匹配项的从零开始索引。

C# List集合类常用操作:三、查找的更多相关文章

  1. selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码

    目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...

  2. mongodb的常用操作(三)

    继续mongodb的学习和总结: 11.mongodb的mapreduce功能 mapreduce可以说是mongodb的一个很强大的功能,可以实现复杂的运算和统计,做一个简要的总结: 假设有user ...

  3. Python脚本控制的WebDriver 常用操作 <三> 浏览器最大化

    下面将模拟执行一个控制浏览器最大化的操作 测试用例场景 当我们在测试中使用一些基于图像和坐标的辅助测试工具时,我们就会需要使浏览器在每次测试时保存最大化,以便在同一分辨率下进行图像比对和坐标点选. 举 ...

  4. Vim 常用操作、查找和替换

    这篇文章来详细介绍 Vim 中查找相关的设置和使用方法. 包括查找与替换.查找光标所在词.高亮前景/背景色.切换高亮状态.大小写敏感查找等. 查找 在normal模式下按下/即可进入查找模式,输入要查 ...

  5. javascript的常用操作(三)

    $.ajax 中的contentType 在 cnodejs.org 论坛中有一个问题,让我也很奇怪,说是 $.ajax 设置数据类型 applicaiton/json之后,服务器端(express) ...

  6. C# List集合类常用操作:四、删除

    Clear() 从 List<T> 中移除所有元素. List<Employees> employees = new List<Employees>(); empl ...

  7. c# List集合类常用操作:二、增加

    所有操作基于以下类 class Employees { public int Id { get; set; } public string Name { get; set; } public stri ...

  8. C# List集合类常用操作 (一)

    所有操作基于以下类 class Employees { public int Id { get; set; } public string Name { get; set; } public stri ...

  9. ORM常用操作

    一般操作 专业官网文档 必会13条查询 <> all(): 查询所有结果 <> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 <> get ...

随机推荐

  1. 2021年最新字节跳动Android面试真题解析

    概述 时间过得是真TM快,回想自己是16年从学校毕业,现在是出来工作的第五个年头啦.在不同的大小公司都待过,就在前段时间顺利的完成了一次跳槽涨薪,面试了几家公司,最终选择了字节跳动.今特此前来跟大家进 ...

  2. CentOS时间日期类语法

    目录 一.date时间日期类 1. date显示当前时间 2. date 显示非当前时间 3. date 设置系统时间 二.cal 查看日历 一.date时间日期类 date [OPTION]... ...

  3. 答应我,安装chromedriver,按照版本号,v70就安装v2.42,

    下载chromedriver,链接:http://chromedriver.storage.googleapis.com/index.html ----------ChromeDriver v2.42 ...

  4. 【Azure 应用服务】App Service .NET Core项目在Program.cs中自定义添加的logger.LogInformation,部署到App Service上后日志不显示Log Stream中的问题

    问题描述 在.Net Core 5.0 项目中,添加 Microsoft.Extensions.Logging.AzureAppServices 和 Microsoft.Extensions.Logg ...

  5. RHEL 7 “There are no enabled repos” 的解决方法

    RHEL 7 "There are no enabled repos"  的解决方法 [root@system1 Desktop]# yum install squidLoaded ...

  6. 如何快速排查发现redis的bigkey?4种方案一次性给到你!

    本篇文章将以redis的bigkey为主题进行技术展开,通过从认识redis的高性能,bigkey的危害.存在原因.4种解决方案,到模拟实战演练的介绍方式,来跟大家一起认识.探讨和学习redis. 先 ...

  7. 浅看spa单页应用路由

    路由观察浏览器的URL的变更.当URL 变更时,路由会解析它并生成一个新的路由实例. 一个基本的路由是这样的: class Router { private _defaultController: s ...

  8. springboot与通用mapper的整合

    找到springboot工程下的pom.xml文件,导入如下的依赖jar包 <!--配置通用Mapper start--> <dependency> <groupId&g ...

  9. Linux中的静态库与动态库

    什么是库文件? 库文件是事先编译好的方法的合集.比如:我们提前写好一些数据公式的实现,将其打包成库文件,以后使用只需要库文件就可以,不需要重新编写. Linux系统中: 1.静态库的扩展名为.a:2. ...

  10. Go版本管理--处理不兼容

    目录 1. 简介 2.能否引起不兼容的包 3.如何处理incompatible 1. 简介 Go module的版本选择机制,其中介绍了一个Module的版本号需要遵循v<major>.& ...