使用Nhibernate和QueryOver添加可选的where参数

时间:2016-10-08 20:55:39

标签: nhibernate queryover

如何在QueryOver中添加可选的where子句?

TL; DR

我正在尝试为应用程序实现搜索表单并使用QueryOver 一些搜索参数是可选的。

          var query =
            myDatabase.QueryOver(() => customerAlias)
                .JoinAlias(() => customerAlias.Projects, () => projectAlias)
                .Where(() => projectAlias.IsClosed >= 1)
                ... possibly add more stuff

1 个答案:

答案 0 :(得分:4)

对于通常的Linq语句,

QueryOver在执行中被推迟。它只会在您通过调用.List<T>()

之类的终结方法强制执行时执行
var query =
    myDatabase.QueryOver(() => customerAlias)
        .JoinAlias(() => customerAlias.Projects, () => projectAlias)
        .Where(() => projectAlias.IsClosed >= 1);

if (myCondition) {
    query = query.Where(...);
}

var result = query.List<T>(); //Or however else you want to make it execute.

您仍然可以通过这种方式访问​​内联别名。