简体   繁体   English

Nhibernate Linq Sum与计算导致NotSupportedException

[英]Nhibernate Linq Sum with calculation causes NotSupportedException

I am using Nhibernate 3.3.3.4001 with SybaseSQLAnywhere12Dialect and trying to use a simple calculation within a sum linq function. 我将Nhibernate 3.3.3.4001与SybaseSQLAnywhere12Dialect一起使用,并尝试在sum linq函数中使用简单的计算。

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. 我想使用Linq而不是HQL来实现这一点,并且很乐意将默认linq的Nhibernate扩展到hql生成器。

Using the following Linq query raises the exception 使用以下Linq查询会引发异常

System.NotSupportedException: Expression type 'NhSumExpression' is not supported by this SelectClauseVisitor System.NotSupportedException:此SelectClauseVisitor不支持表达式类型'NhSumExpression'

Linq Query Linq查询

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

Expected SQL Statement 预期的SQL语句

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. 如果在没有Sum()函数的情况下运行相同的查询,则它会正确执行计算,因此很困惑为什么Sum()无法正常工作。

You may try to rewrite your LINQ query a bit, just like this one: 您可以尝试重新编写LINQ查询,如下所示:

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

There is an open issue when using constants within Linq group function (Max, Min, Sum) functions. 在Linq组函数(最大值,最小值,和)中使用常量时,存在一个未解决的问题。 If another field is used it will work correctly. 如果使用其他字段,它将正常工作。

Found bug within NHibernate 3.3.3 在NHibernate 3.3.3中发现错误
https://nhibernate.jira.com/browse/NH-3376 https://nhibernate.jira.com/browse/NH-3376

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM