How to negate a delegate?

.net, c#, delegates, lambda, linq

Solution

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();

Your stack overflow issue is pretty obvious; you're defining `selector` in terms of itself1. The helper method avoids that problem.

1: That is, this will clearly cause a stack overflow too:

public bool M() { return !M(); }

Believe it or not, you're doing exactly the same thing.

Problem

This is my code, heavily abbreviated for simplicity ``` Func<Product, bool> selector; ... selector = p => p.IsNew; ... if(negative) // not selector selector = x => !selector(x); // This is wrong (causes infinite loop) // How do you do negate it? The result should be p => !p.IsNew ... IEnumerable<Product> products = MyContext.Products.Where(selector); ```

Original source