Restrictions.Disjunction() between condition a AND condition b OR condition c AND condition d

Chris Conway picture Chris Conway · Aug 14, 2009 · Viewed 8.1k times · Source

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!

Answer

sirrocco picture sirrocco · Aug 14, 2009

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))
 );
                                                                )