3

I am trying to express the following SQL query with NHibernate

DECLARE @date DATETIME = NULL;

SELECT 
    ER.Id
,   ER.DocumentDate
FROM 
    ExpenseReport ER
WHERE
    ER.PeriodFrom >= COALESCE(@date, ER.PeriodFrom)
OR ER.PeriodTo <= COALESCE(@date, ER.PeriodTo);

So, in the C# part I do have the following classes:

  • for the entity : ExpenseReport
  • for my search itself a separate class

Code snippets:

// ----- Entity class.
public partial class ExpenseReport
{
    public Nullable<System.DateTime> PeriodFrom { get; set; }
    // many other properties
}

// ----- Search parameter class.
public class SearchParameters
{
    public Nullable<System.DateTime> DateFrom { get; set; } 
    // many other properties
}

So, assigning now the search parameters to IQueryOver<ExpenseReport>

var q = SessionProvider.QueryOver<ExpenseReport>();

And I am a bit lost now with NHibernate .... How do I do it now?

q.And( /*** I AM STUCK HERE **/)
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Dimi Takis
  • 4,924
  • 3
  • 29
  • 41

1 Answers1

4

A drafted code should look like this:

// left side
var left = Projections.Property<ExpenseReport>(ti => ti.PeriodFrom);
// right side
var right = Projections.SqlFunction("COALESCE"
        , NHibernateUtil.DateTime
        , Projections.Constant(search.DateFrom, NHibernateUtil.DateTime)
        , Projections.Property<ExpenseReport>(ti => ti.PeriodFrom)
    );
// the restriction using the GeProperty, taking two IProjections
var restriction = Restrictions.GeProperty(left, right);

// finally - our query get its WHERE
q.Where(restriction);

So, we firstly create two projections. Then we used the Restrictions utility set to create the >= (GeProperty). Resulting restriction is finally passed into WHERE clause...

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Great lead...I posted a follow-up question referring to this answer in hopes to gain a little more clarification on the topic : http://stackoverflow.com/questions/29221666/nhibernate-comparison-constraint-to-a-coalesced-date – beauXjames Mar 23 '15 at 22:34
  • *In case I read the question correctly, I tried to show how to...* – Radim Köhler Mar 24 '15 at 05:14