Nhibernate Linq Sum with calculation causes NotSupportedException

柔情痞子 提交于 2019-12-24 07:49:36

问题


I am using Nhibernate 3.3.3.4001 with SybaseSQLAnywhere12Dialect and trying to use a simple calculation within a sum linq function.

I would like to use Linq rather than HQL to achieve this and would be happy to extend Nhibernate with a default linq to hql generator.

Using the following Linq query raises the exception

System.NotSupportedException: Expression type 'NhSumExpression' is not supported by this SelectClauseVisitor

Linq Query

var query = (from c in session.Query<Entity>()
            select c.FieldValue / 100M).Sum()

Expected SQL Statement

SELECT SUM(FieldValue / 100.0) FROM TableName

If the same query is run without the Sum() function it performs the calculation correctly, as such am confused why the Sum() wouldn't work.


回答1:


You may try to rewrite your LINQ query a bit, just like this one:

var result = ((from c in session.Query<Entity>()
               select c.FieldValue).Sum()) / 100;

Resulting query could also be optimized by moving division operator out of the sum:

SELECT SUM(FieldValue) / 100 FROM TableName



回答2:


There is an open issue when using constants within Linq group function (Max, Min, Sum) functions. If another field is used it will work correctly.

Found bug within NHibernate 3.3.3
https://nhibernate.jira.com/browse/NH-3376



来源:https://stackoverflow.com/questions/22909011/nhibernate-linq-sum-with-calculation-causes-notsupportedexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!