tags:

views:

1505

answers:

2

Hi folks!

When creating a criteria for NHibernate all criteria are added as AND.

For instance:

session.CreateCriteria(typeof(someobject))
.Add(critiera)
.Add(other_criteria)

then end result will be

SELECT ... FROM ... WHERE criteria AND other_criteria

I would like to tell NHibernate to add the criterias as "OR"

SELECT ... FROM ... WHERE criteria OR other_criteria

Any help is appreciated

/Regards Vinblad

A: 

You could use Restrictions.or, such that:

session.CreateCriteria(typeof(someobject))
    .Add(critiera)
    .Add(other_criteria);

where:

other_criteria = Restrictions.or("property", "value");

You can learn more about this following the Criteria Interface documentation of Hibernate, which is the same as NHibernate.

Jon Limjap
+14  A: 

You're looking for the Conjunction and Disjunction classes, these can be used to combine various statements to form OR and AND statements.

AND

.Add(
  Expression.Conjunction()
    .Add(criteria)
    .Add(other_criteria)
)

OR

.Add(
  Expression.Disjunction()
    .Add(criteria)
    .Add(other_criteria)
)
James Gregory