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

 

Clear()    从 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") });
Console.WriteLine("Clear清空前" + employees.Count + " " + employees.Capacity);  //Count=5  Capacity = 8
employees.Clear(); //Count=0 
employees.TrimExcess(); //Capacity = 0
Console.WriteLine("Clear清空后" + employees.Count + " " + employees.Capacity);
Remove(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") });
Console.WriteLine("Remove前" + employees.Count);      //Count = 5      

var emp = employees.FirstOrDefault(e => e.Id == 5);
//如果成功移除了 true,则为 item;否则为 false。 如果在 false 中没有找到 item,则此方法也会返回 List<T>。
if (employees.Remove(emp))
{
    Console.WriteLine("Remove后" + employees.Count);  //Count = 4  
}
RemoveAll(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") });
Console.WriteLine("RemoveAll前" + employees.Count);      //Count = 5      
//遍历list中的元素,将符合lambda表达式结果的元素全部删除掉,返回结果是删除掉的元素个数。
Console.WriteLine("RemoveAll后共删除了" + employees.RemoveAll(e => e.Id >= 3) + "条数据");  //RemoveAll后共删除了3条数据

RemoveAt(Int32)    移除 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") });
Console.WriteLine("Remove前" + employees.Count);      //Count = 5      
//根据下标删除待删除元素的从零开始的索引。
employees.RemoveAt(4);     //Count = 4 
//查找删除
var index = employees.FindIndex(e => e.Id == 5);
employees.RemoveAt(index);
Console.WriteLine("Remove后" + employees.Count);  //Count = 4  
RemoveRange(Int32, Int32)    从 List<T> 中移除一系列元素。
index Int32 要移除的元素范围的从零开始的起始索引。
count Int32 要移除的元素数。
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") });
 Console.WriteLine("RemoveRange" + employees.Count); //Count = 5 
//根据索引,删除指定条数元素
var index = employees.FindIndex(e => e.Id == 1);
employees.RemoveRange(index,
3);
Console.WriteLine(
"RemoveRange后" + employees.Count); //Count = 2

 

posted @ 2020-04-24 12:47  liessay  阅读(10485)  评论(0编辑  收藏  举报