System.NotSupportedException:如何简化NHibernate查询?
问题描述:
如何在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.Nullable1[System.Decimal])([100001].Prod2.Rate), Void Add(System.Nullable
1 System.Decimal])([100001] .Prod3.Rate)}”, “exceptionType”: “System.NotSupportedException”,
我如何简化此查询因为逻辑是完美的T'
答
原因是由于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
可以显示所使用的变量的声明和类型 –
'results'无非是IQueryable的''(什么是'results'?)。一个物业有'Prod1','Prod2','Prod3'作为其属性 –
Karthik
好吧谢谢你,我要去看看我能否找到解决方案 –