zoukankan      html  css  js  c++  java
  • C# List集合类常用操作:三、查找

    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/C++,系统知识考点
    API进程线程函数
    做WEB2.0网站可以参考的十九条规则
    javascript中动态添加事件!!
    常用正则表达式收集!
    CuteChat for Community Server 2.0 beta 3!
    发现一个下载.Text Skin 的好网站.
    如何控制Linux终端打印字符颜色和位置
  • 原文地址:https://www.cnblogs.com/liessay/p/12766081.html
Copyright © 2011-2022 走看看