RemoveAll()
ICollection interface has List type most of time, so this Extension allows to call RemoveAll() method with the same signature like on List
Source
using System;
using System.Collections.Generic;
using System.Linq;
public static class CollectionExtensions
{
public static void RemoveAll<T>(this ICollection<T> @this, Func<T, bool> predicate)
{
List<T> list = @this as List<T>;
if (list != null)
{
list.RemoveAll(new Predicate<T>(predicate));
}
else
{
List<T> itemsToDelete = @this
.Where(predicate)
.ToList();
foreach (var item in itemsToDelete)
{
@this.Remove(item);
}
}
}
}
Example
ICollection<int> items = new List<int>(new[] { 1, 3, 5 });
items.RemoveAll(x => x > 2);
ICollection<int> linkedItems = new LinkedList<int>(new[] { 1, 3, 5 });
linkedItems.RemoveAll(x => x > 2);