1

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!

Chris Conway
  • 16,269
  • 23
  • 96
  • 113

2 Answers2

6

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))
 );
                                                                )
sirrocco
  • 7,975
  • 4
  • 59
  • 81
  • I ended up doing it completely differently from this because of the dynamic nature of my query builder, but this does answer the question as stated. Thanks! I used the operator overload features of nhibernate to create an AbstractCriterion and then added all the abstractcriterion to a Restrictions.Disjunction(). – Chris Conway Aug 14 '09 at 14:28
2

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)))
johnny
  • 224
  • 2
  • 12