List<>的常用方法(未完,随时积累)

这里记录一些List<>集合的常用方法,随时学习随时积累。


1、Find方法

泛型集合List<T>中的Find函数用于查找集合中符合指定条件的元素..相比foreach遍历元素,用Find函数查找,代码更简洁.

函数原型如下:

public T Find(Predicate<T> match);

其中Predicate为C#定义好的委托,原型如下:

public delegate bool Predicate<in T>(T obj);

所以,List.Find函数的参数,就是一个 返回值为bool,入参为T类型的函数。当然,可以是命名函数也可以是匿名函数或Lambda表达式.。

使用方法如下:

//定义一个Person类

class Person

    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
    }
 
class Program
    {
        //定义一个Predicate类型的委托变量
        private static Predicate<Person> pre = new Predicate<Person>(MyPredicate);
        private static bool MyPredicate(Person p)
        {
            bool result = false;
            if (p.Name == "张三")
                result = true;
            return result;
        }
        static void Main(string[] args)
        {
            try
            {
                 List<Person> lstPerson = new List<Person>()
                {
                new Person { Name = "张三", Age = 10, Gender = "M" },
                new Person { Name = "李四", Age = 11, Gender = "M" },
                new Person { Name = "王五", Age = 12, Gender = "M" },
                new Person { Name = "赵六", Age = 13, Gender = "M" },
                new Person { Name = "张三", Age = 33, Gender = "F" }
                };
                Person p1 = lstPerson.Find(pre);//1、命名函数
                Person p2 = lstPerson.Find(delegate (Person s) { return s.Name.Equals("王五"); });//2、匿名函数
                Person p3 = lstPerson.Find(s => { return s.Name.Equals("赵六"); });//3、Lambda表达式
              Person p4 = lstPerson.Find(s => s.Name.Equals("赵六"));//4、Lambda表达式的简洁写法
                Console.WriteLine($"姓名:{p1.Name},年龄:{p1.Age}性别:{p1.Gender}");
                Console.WriteLine($"姓名:{p2.Name},年龄:{p2.Age}性别:{p2.Gender}");
                Console.WriteLine($"姓名:{p3.Name},年龄:{p3.Age}性别:{p3.Gender}");
            }
            catch (Exception ea)
            {
                Console.WriteLine($"异常:{ea.Message}");
            }
            Console.ReadKey();
        }
    }

总结:

        List<T>.Find 返回的是符合条件的一个元素.若没有,则返回T类型的默认值

        List<T>.FindLast  返回符合条件最后一个元素.若没有,则返回T类型的默认值

        List<T>.FindAll 返回符合条件的元素集合,即List<T>的子集.

        List<T>.FindIndex 返回符合条件的第一个元素的下标.下标从0开始.. 若没有,则返回-1

        List<T>.FindLastIndex 返回符合条件的最后一个元素的下标.下标从0开始.. 若没有,则返回-1

 


 

2、判断两个List集合内容是否相同

  list.All()方法,

  list.SequenceEqual()方法

List<byte> l1=new List<byte>{0x01,0x02,0x03}
 
List<byte> l2=new List<byte>{0x01,0x02,0x03}
 
//上面l1和l2内容是一样的
 
l1==l2;//这样返回为假
l1.Equals(l2);//这样也为假
l1.SequenceEqual(l2);//这样才为真   Linq 自带函数SequenceEqual 查看内容是否一样
 
 
//====================================================================================
//2019-05-02 更新
//有的时候  两个集合内容一样 但是  集合内元素顺序不一样,用以上方法判断都是false
//以下是解决  判断两个集合的所有元素都否都一样
List<byte> l3=new List<byte>{0x11,0x22,0x03}
List<byte> l4=new List<byte>{0x22,0x11,0x03}
 
bool b1 = l4.All(l3.Contains);
//判断l3里面是否都包含l4里面的所有元素这里为true
//如果l3里面的元素为{0x11,0x22,0x03,0x04} 比l4{0x22,0x11,0x03}多一个元素  这里也为true 
//如果l3里面的元素为{ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 } 比l4{0x22,0x11,0x03}多6一个元素  这里也为true 
//如里l4里面元素为{0x22,0x11,0x03,0x04} l3里面元素为{0x11,0x22,0x03} 则b1为false
 
bool b3 = l3.SequenceEqual(l4); //结果为false
//l3和l4里面内容都一样 但顺序不一样 如果像上面 l1和l2内容和顺序也一样则为true

 


3、where方法

where方法,根据条件查找list集合并将查找结果集返回一个list集合.

where方法和find方法类似,如果查找的返回结果确定只有一个,优先使用find,因为where返回一个list集合。

其次,注意值类型与引用类型。

如下代码所示的结果:

using System;
using System.Collections.Generic;
using System.Linq;

namespace list集合的查找与修改
{
    /// <summary>
    /// 创建一个Book类
    /// </summary>
    public class Book
    {
        public string BookName { get; set; }
        public string BarCode { get; set; }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            List<Book> listBook = new List<Book>();
            for (int i = 0; i < 10; i++)
            {
                Book book = new Book();
                book.BookName = "书名" + i;
                book.BarCode = "条形码" + i;
                listBook.Add(book);
            }

            var books = listBook.Where(p => p.BarCode == "条形码6");//查询BarCode==条形码6;            
            if (books != null && books.Count() > 0)
            {
                Book book = books.ToArray()[0];
                book.BookName = "西游记";//修改书名               
            }

            var b1 = listBook.Find(p => p.BarCode == "条形码1");
            Book myBook = b1 as Book;
            myBook.BookName = "水浒传";

            foreach (var item in listBook)
            {
                Console.WriteLine(item.BookName + ":" + item.BarCode);
            }
            Console.ReadLine();
        }
    }
}

 

运行结果:

 

 

posted on 2022-12-14 11:36  hanzq_go  阅读(1100)  评论(0编辑  收藏  举报

导航