Take the 2-minute tour ×

In all the examples I've seen for predicate builder it shows a starting expression with PredicateBuilder.True if you are building an "and" expression criteria and PredicateBuilder.False if you are building an "or" expression criteria.

My questions is, is this always the case, and if so why couldn't this be simply inferred. I suspect there must be cases where you are be building an "and" expression and want to start with a false. And the opposite for an "or"

Can anyone explain this to me?

share|improve this question
add comment

1 Answer

In the expression A and B and C and D, if any of the conditions (A, B, C, or D) are False, the entire expression is False. So if you start an "and" expression with False, no matter what else you AND to it, the entire expression will be False.

Same with an "or" expression. In the expression A or B or C or D, if any of the conditions (A, B, C, or D) are True, the entire expression is True. So if you start an "or" expression with True, no matter what else you OR to it, the entire expression will be True.

As for why you need to start a PredicateBuilder with the literal True or False, I believe this was simply a convention to make using PredicateBuilder easier. Your expressions always start with a (boolean) condition, followed by 0 or more "And/Or condition" parts. So you can have "A", or "A and B", or "A and B and C". You never start with "and A". The .And(condition) of PredicateBuilder adds "and condition" to the expression, so you can't start with it. There could have been a constructor that let your create your Expression and start it with an initial condition (new Expression<...>(A)? I haven't actually checked... is there one?) but then you'd have to treat your first condition (you're probably looping over some collection of things and adding to your Expression) differently (calling a constructor) than you do your 2nd and subsequent conditions (calling .And(...) or .Or(...)). The .True<...>() and .False<...>() methods of PredicateBuilder handle creating your Expression as well as adding the first (generic) condition so that you can handle adding your 1st and all subsequent conditions in your loop the same (by calling .And(...) or .Or(...)).

share|improve this answer
add comment

Your Answer


