Grouping in state discarded

LINQ to NHibernate removes the brackets in the where section:

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

As a result, the following query appears (note the missing bracket):

 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 significantly changes the query condition.

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

+6
source share
2 answers

Since AND has a higher priority in the order of operations on OR, parentheses are not needed, therefore, the query provider does not add redundant information.

To remember the order of operations, logical AND is considered similar to multiplication (by binary value), and OR is considered similar to adding by binary values. When working with Boolean algebra (in a non-programming environment) it is actually quite common to use * for AND and + for OR.

+5
source

AND takes precedence over OR , just as multiplication takes precedence over addition.

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

+1
source

Source: https://habr.com/ru/post/908532/


All Articles