Search code examples
c#.netexpression-treespredicatebuilder

PredicateBuilder is still true even after adding explicit false statement


I have the following statement.

var search = PredicateBuilder.True<SomeType>();
search.And(f => false);

// Still the "search" variable value is: {f => true}.

At first, I was trying an expression; nothing succeeded, so I tried this "false" instead. No matter what I did, it's still the same. What is happening?


Solution

  • PredicateBuilder methods don't mutate the expression (they couldn't, as expression trees are immutable) - you need to use the return value:

    var search = PredicateBuilder.True<SomeType>();
    search = search.And(f => false);