如何将此存储过程转换为HQL Nhibernate?

时间:2013-05-31 17:01:02

标签: c# sql-server-2008 fluent-nhibernate

我有一个存储过程,我必须将其转换为Nhibernate创建查询。 Procedure有一个CASE子句。程序是:

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 

1 个答案:

答案 0 :(得分:1)

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>();