2013-05-31 33 views
0

我有一個存儲過程,我必須將其轉換爲Nhibernate創建查詢。過程有一個CASE子句。程序是:如何將此存儲過程轉換爲HQL Nhibernate?

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 
+0

是HQL的要求或是QueryOver一個選項 – Firo

+0

HQL或創建標準是要求。 –

+0

這對Stack Overflow來說太廣泛了 - 雖然問題包含代碼,但它是「爲我轉換此代碼」,這些日子我們試圖阻止。 '我不知道這是否會在2013年':-)'主題 – halfer

回答

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