Daniel Hilgarth
Daniel Hilgarth

Reputation: 174369

Grouping in condition is being dropped

LINQ to NHibernate removes parenthesis in where clause:

session.Query<MyEntity>().Where(x => (x.MyProp1 < end && x.MyProp1 > start) ||
                                     (x.MyProp2 < end && x.MyProp2 > start));

This results in the following query (note the missing parenthesis):

select <columns> from MY_ENTITY where MY_PROP1 < :p0 and MY_PROP1 > :p1 or 
                                      MY_PROP2 < :p2 and MY_PROP2 > :p3;

This is a huge problem, because it changes the query condition significantly.

Is this a known problem or am I doing something wrong?

Upvotes: 6

Views: 226

Answers (2)

Servy
Servy

Reputation: 203828

Because AND has a higher precedence in order of operations over OR the parenthesis are not needed, so the query provider doesn't add in the redundant information.

To help remember orders of operations, a boolean AND is considered analogous to multiplication (on a binary value) and OR is considered analogous to addition on binary values. When dealing with boolean algebra (in a non-programming environment) it is actually not uncommon to use * for AND and + for OR.

Upvotes: 5

Diego Mijelshon
Diego Mijelshon

Reputation: 52735

AND has higher precedence than OR, just like multiplication has higher precedence than addition.

Therefore, the parentheses are redundant and do not exist in the expression tree.

Upvotes: 1

Related Questions