LosManos
LosManos

Reputation: 7692

Adding optional where parameters with Nhibernate and QueryOver

How do I add optional where clauses with QueryOver?

TL;DR

I am trying to implement a search form for an application and use QueryOver.
Some of the search parameters are optional.

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

Upvotes: 0

Views: 862

Answers (1)

starlight54
starlight54

Reputation: 1091

QueryOver is deferred in execution as for usual Linq statements. It will only execute when you force it to by calling a finalising method such as .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.

You should still be able to access the in-line aliases too this way.

Upvotes: 4

Related Questions