【问题标题】:NHIbernate add OR Criteria Query to existing criteriasNHIbernate 将 OR 条件查询添加到现有条件
【发布时间】:2014-07-31 10:16:01
【问题描述】:

我知道如何使用Disjunction() 添加 OR 限制,但我需要添加到这组标准中,而且我总是需要至少 2 个标准。我想知道如何将单个 OR 添加到现有条件。

半代码示例。

model.RootCriteria.Add(restriction)

接下来几行我想根据变量添加 AND 或 OR 限制到已经存在的变量。

if(AND) model.(Here add AND restriction)

else if(OR) model.(Here add OR restriction) 我不知道如何这样做

我想要接收的输出是:

Where name like 'name ' (AND or OR) second restriction;

【问题讨论】:

    标签: c# sql hibernate nhibernate queryover


    【解决方案1】:

    我们使用标准的方式始终是.Add() 操作,它采用ICriterion

    var criteria = session.CreateCriteria<Employee>();
        criteria.Add(
          Restrictions.On<Employee>((x) => x.LastName).IsLike("Xyz", MatchMode.Start)
        );
    

    即使我们想使用 OR,我们也必须通过 .Add() 添加它,如下所示:

        criteria.Add(Restrictions.Or(
            Restrictions.On<Employee>((x) => x.FirstName).IsLike("Abc", MatchMode.Start)
            , Restrictions.On<Employee>((x) => x.FirstName).IsLike("Def", MatchMode.Start)
        ));
    

    诀窍在于Restrictions.Or 也返回ICriterion...

    还要检查这些:

    【讨论】:

    • 你的例子是where lastName like 'Xyz' AND ( firstName like 'Abc' OR firstName like 'Def')。我只想在声明中添加 OR 到当前的标准。为什么没有简单的 .AddOR() 和 .AddAnd() 函数?
    • 这是一个例子。因此,为您提供解决方案:首先检查哪些两个语句必须用 OR 连接。然后,将它们放入 Restrictions.Or 并将结果 .Add() 放入标准中。没有其他方法...但这对我们中的许多人都有效...很长一段时间;)另外,请查看我指向Disjunction的链接...如果您有更多陈述
    猜你喜欢
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2016-09-22
    • 2020-11-15
    • 2022-11-08
    • 1970-01-01
    相关资源
    最近更新 更多