System.NotSupportedException: 如何简化NHibernate Query?

System.NotSupportedException: How to simplify the NHibernate Query?

如何在 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": "new List1() {Void Add(System.Nullable1[System.Decimal])([100001].Prod1.Rate), Void Add(System.Nullable1[System.Decimal])([100001].Prod2.Rate), Void Add(System.Nullable1[System.Decimal])([100001].Prod3.Rate)}", "exceptionType": "System.NotSupportedException",

逻辑完美的我如何简化这个查询?

原因是 NHibernate 无法从您的 Linq 生成 SQL,因为 List 初始化是。

试试这个:

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;