I have a list of predicates
public List<Func<Album, bool>> Predicates { get; set; }
I'd like to check if a list contains specific predicate.
What I do is this :
bool check = Predicates.Contains(x=>x.AlbumName == "Winter");
But this always returns false even though there is such a predicate in the list. I assume it is because predicates are anonymous methods and each is kind of unique, but still is it possible to compare them somehow?
I'm afraid the answer is basically "no". If you had expression trees instead of delegates then you could probably compare those with effort, but basically you've got references to separate methods. You'd need to inspect the IL inside the methods to compare whether or not they're the same.
Of course if you have a set of objects on which the predicates operate, you could find out whether you have any predicates which matches the same subset as your "target" predicate, but that's not the same as testing whether the predicate is actually the same.
See more on this question at Stackoverflow