2

I'm writing a notification platform using C# and NHibernate. I'm having difficulties with my queries.

I have a Customer entity - which contains an AssessmentCompleted Property. A notification should be sent out 21 months after certification. So my query needs to include all customers where their AssessmentCompletedDate + 21months < currentDate. How do I achieve this? Is there a month add method in NHibernate? I need to add 21 months to each AssessmentCompletedProperty. My query needs to look something like:

SELECT new Notification(c.Id, c.Description, c.AssessmentCompleted + 21
            FROM Cusomter c 
            AND c.AssessmentCompleted + 21 <= :EndDate
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MegaByte
  • 6,725
  • 10
  • 37
  • 33

1 Answers1

3

You can inherit from the dialect you use and register the function.

For example, for MS SQL you can register the dateadd function like this:

public class MyExtendedMsSql2012Dialect : MsSql2012Dialect
{
    protected override void RegisterFunctions()
    {
        base.RegisterFunctions();

        RegisterFunction("dateadd", new SQLFunctionTemplate(NHibernateUtil.Date, "dateadd(?1, ?2, ?3)"));
    }
}

Then if you configure that dialect you'll be able to use it in HQL and with the Criteria API.

bounav
  • 4,886
  • 4
  • 28
  • 33
Fabio Maulo
  • 427
  • 4
  • 3