简体   繁体   English

如何为NHibernate创建OR语句?

[英]How to create OR statements for NHibernate?

When creating a criteria for NHibernate all criteria are added as AND . 为NHibernate创建条件时,所有条件都添加为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" 我想告诉NHibernate将标准添加为“OR”

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

Any help is appreciated 任何帮助表示赞赏

You're looking for the Conjunction and Disjunction classes, these can be used to combine various statements to form OR and AND statements. 您正在寻找ConjunctionDisjunction类,这些类可用于组合各种语句以形成OR和AND语句。

AND

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

OR 要么

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

Use Restrictions.Disjunction() 使用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);

You could use Restrictions.or , such that: 您可以使用Restrictions.or ,这样:

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. 您可以按照HibernateCriteria Interface文档了解更多信息,这与NHibernate相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM