You can do this with a helper method:
public static Predicate<T> Negate<T>(this Predicate<T> predicate) { return t => !predicate(t); }
(or replace Predicate with Func<T, bool> ).
Then:
selector = selector.Negate();
The stack overflow problem is pretty obvious; you define selector in terms of yourself 1 . A helper method avoids this problem.
1 : That is, this will explicitly cause a stack overflow:
public bool M() { return !M(); }
Believe it or not, you are doing the same.
source share