views:

1286

answers:

2

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!

+2  A: 

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
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
A: 

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