问题
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