Of course. You would do it like this:
public List<T> GetAll<T>(List<T> list, Func<T, bool> where) { return list.Where(where).ToList(); }
You would call it like this:
var result = GetAll(AllElements, o => o.ToString() == "lala");
You can even create it as an extension method:
public static List<T> GetAll<T>(this List<T> list, Func<T, bool> where) { return list.Where(where).ToList(); }
and name it as follows:
var result = AllElements.GetAll(o => o.ToString() == "lala");
But actually, in your simple example, this makes no sense, because it is exactly the same as directly using Where
:
var result = AllElements.Where(o => o.ToString() == "lala").ToList();
However, if your GetAll
method performs some other actions, it makes sense to pass a predicate to this method.
source share