سؤال

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?

هل كانت مفيدة؟

المحلول

Just do this:

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

if (ids == null || ids.Count == 0)
{
    query = query.WhereRestrictionOn(x => x.OrderId).IsIn(Ids);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top