Search code examples
c#-4.0linq-to-sqlfluent-nhibernate

NHibernate queryover how to apply date isbetween without including the from and todate


I am trying to write NHibernate queryover to select all records which has been deleted between two dates. I am using IsBetween().And(). But how do i write if i dont want to include both the fromdate and todate?

Here is my query:

public IEnumerable<DeletedRecord> Search(
    DateTime deletedFrom,
    DateTime deletedTo
    )
{
    DeletedRecord delAlias = null;
    var query = Session.QueryOver(() => delAlias);
    query.Where(() => delAlias.DeletedDate.IsBetween(deletedFrom).And(deletedTo));

    return query.Future<DeletedRecord>();
}

Can anyone help me how to achieve so that i can bring all records after the deletedFrom date and before the deletedTo date?

Thanks


Solution

  • Just construct your date in 2 steps:

    var query = Session.QueryOver(() => delAlias);
    
    if(youNeedFromDate) //first step
       query = query.Where(() => delAlias.DeletedDate >= deletedFrom);
    if(youNeedToDate) //second step
       query = query.Where(() => delAlias.DeletedDate <= deletedTo);
    

    youNeedFromDate and youNeedToDate are bool variables that you can pass to your function or it could be different condition upon your logic.