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

本文关键字:HQL Nhibernate 转换 存储过程 | 更新日期: 2023-09-27 18:16:13

我有一个存储过程,我必须将其转换为Nhibernate创建查询。过程有一个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 

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

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