Search code examples
c#nhibernatenhibernate-mappinglinq-to-nhibernatenhibernate-criteria

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?


Solution

  • Just do this:

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