user1527762
user1527762

Reputation: 957

NHibernate using optional where

Is there a way in Nhibernate to query with an optional where clause.

I have a query below with a list of Ids passed in:

var query = Session.QueryOver<Orders>()
                   .WhereRestrictionOn(x => x.OrderId)
                   .IsIn(Ids);

If the list is empty I would like to return the whole Orders table. Is this possible to do this using NHiberbate?

Upvotes: 1

Views: 368

Answers (1)

sergiogarciadev
sergiogarciadev

Reputation: 2202

Just do this:

var query = Session.QueryOver<Orders>();

if (ids == null || ids.Count == 0)
{
    query = query.WhereRestrictionOn(x => x.OrderId).IsIn(Ids);
}

Upvotes: 3

Related Questions