2017-06-16 55 views
0

如何在NHibernate中簡化下面的查詢?System.NotSupportedException:如何簡化NHibernate查詢?

以下是查找3種不同產品之間的最大值並按最大值對其排序的邏輯。

IQueryable<Property> results = ISession.Get<Property>(); 

results = from r in results 
      let pdts = new List<decimal?> { r.Prod1.Rate, r.Prod2.Rate, r.Prod3.Rate } 
      let max = pdts.Max() 
      orderby max 
      select r; 

當執行它,NHibernate的拋出錯誤System.NotSupportedException與exceptionMessage

「exceptionMessage」:「新列表1() {Void Add(System.Nullable 1 [System.Decimal])([100001] .Prod1.Rate) ,太虛添加(System.Nullable 1[System.Decimal])([100001].Prod2.Rate), Void Add(System.Nullable 1 System.Decimal])([100001] .Prod3.Rate)}」, 「exceptionType」: 「System.NotSupportedException」,

我如何簡化此查詢因爲邏輯是完美的T'

+0

可以顯示所使用的變量的聲明和類型 –

+0

'results'無非是IQueryable的''(什麼是'results'?)。一個物業有'Prod1','Prod2','Prod3'作爲其屬性 – Karthik

+1

好吧謝謝你,我要去看看我能否找到解決方案 –

回答

3

原因是由於List初始化,NHibernate無法從Linq生成SQL。

試試這個:

results = from r in results 
      let max = (r.Prod1.Rate >= r.Prod2.Rate && r.Prod1.Rate >= r.Prod3.Rate) ? r.Prod1.Rate 
        : (r.Prod2.Rate >= r.Prod1.Rate && r.Prod2.Rate >= r.Prod3.Rate) ? r.Prod2.Rate 
        : r.Prod3.Rate 
      orderby max 
      select r; 
+1

謝謝。我能夠用你的解決方案來實現 – Karthik