Search code examples
c#sql-server-2008fluent-nhibernate

How to convert this stored procedure into HQL Nhibernate?


I have a stored procedure that I have to convert it into Nhibernate create query. Procedure has a CASE clause. Procedure is:

Select * From tDRMaster  
 Where fDate =  
    Case When @Date IS NULL Then (Select Max(fDate) From tDRMaster Where fPropertyID = @PropertyID)  
    Else @Date  
   End  
   And fPropertyID = @PropertyID 

Solution

  • var results = session.CreateCriteria<DrMaster>()
        .Add(Expression.EqProperty("fDate",
            Projections.Conditional(Expression.Eq("Date", null), 
                Projections.SubQuery(DetachedCriteria.For<DrMaster>()
                    .Add(Expression.EqProperty("fPropertyId", "PropertyId"))
                    .SetProjection(Projections.Max("fDate"))),
                Projections.Property("Date"))))
        .Add(Expression.EqProperty("fPropertyId", "PropertyId"))
        .List<DrMaster>();