Restrictions.Disjunction() between condition a AND condition b OR condition c AND condition d
-
16-09-2019 - |
Question
How can I create a disjunction in NHibernate that would accomplish the following sql:
Select * from MyTable
Where (conditionA = true AND conditionB = true)
OR (conditionC = true AND conditionD = true)
From what I've seen, the Disjuntion() takes single criterions and "ORs" them together. Is it possible to group to criterion together and "OR" it against another pair of criterion?
I hope this question is clear enough.
Thanks!
Solution
It's not exactly pretty but you would write it like this :
.Add(
Restrictions.Or(
Restrictions.Conjunction().Add(Restrictions.Eq("columnA", true)).Add(Restrictions.Eq("columnB", true)),
Restrictions.Conjunction().Add(Restrictions.Eq("columnC", true)).Add(Restrictions.Eq("columnD", true))
);
)
OTHER TIPS
You also can use
.Add(
Expression.Or(
Expression.And(Expression.Eq("columnA",true), Expression.Eq("columnB",true)),
Expression.And(Expression.Eq("columnC",true), Expression.Eq("columnD",true)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow