Cómo crear O declaraciones para NHibernate?


Al crear un criterio para NHibernate todos los criterios se agregan como AND.

Por ejemplo:

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

Entonces el resultado final será

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

Me gustaría decirle a NHibernate que agregue los criterios como"O"

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

Cualquier ayuda es apreciada

 22
Author: Mahmoud Gamal, 2008-12-22

3 answers

Está buscando las clases Conjunction y Disjunction, estas se pueden usar para combinar varias declaraciones para formar declaraciones OR and and.

Y

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

O

.Add(
  Expression.Disjunction()
    .Add(criteria)
    .Add(other_criteria)
)
 50
Author: James Gregory,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-12-22 14:15:47

Use Restrictions.Disjunction()

        var re1 = Restrictions.Eq(prop1, prop_value1);
        var re2 = Restrictions.Eq(prop2, prop_value2);
        var re3 = Restrictions.Eq(prop3, prop_value3);

        var or = Restrictions.Disjunction();
        or.Add(re1).Add(re2).Add(re3);

        criteria.Add(or);
 14
Author: savemaxim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-09 13:23:19

Podrías usar Restrictions.or, de manera que:

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

Donde:

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

Puede obtener más información sobre esto siguiendo la documentación de la Interfaz de criterios de Hibernate, que es la misma que NHibernate.

 3
Author: Jon Limjap,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-12-22 14:13:14