I’m trying to create an extension method called RemoveWhere that removes an item from a List collection based on a predicate. For example
var result = products.RemoveWhere(p => p.ID == 5);
I’m using Microsoft’s Where extension method signature as a starting point. Here’s what I have so far:
public static List<T> RemoveWhere<T>(this List<T> source, Func<T, List<T>> predicate)
{
if (source == null)
{
throw new ArgumentNullException("source", "The sequence is null and contains no elements.");
}
if (predicate == null)
{
throw new ArgumentNullException("predicate", "The predicate function is null and cannot be executed.");
}
// how to use predicate here???
}
I don’t know how to use the predicate. Can someone help me finish this? Thank you!
The Predicate parameter should be:
Func<T,bool>EDIT: As others have pointed out, this method is either misnamed, or it already exists on List. My guess is just that you’re trying to understand how a passed in delegate is used by the method itself. For that you can look at my sample. If that is not your intent, I’ll delete this answer as the code really is kind of pointless.