Linq Sum()精度

Dou*_*ers 5 c# mysql linq nhibernate floating-point-precision

在我的项目中,我一直在使用Linq's Sum()很多东西.它由MySQL上的NHibernate提供支持.在我的Session Factory我已经明确要求NHibernate的处理正好是8位小数,当涉及到decimals:

public class DecimalsConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        if (instance.Type.GetUnderlyingSystemType() == typeof(decimal))
        {
            instance.Scale(8);
            instance.Precision(20);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我发现将.Sum()小数点后5位的数字四舍五入:

var opasSum = opasForThisIp.Sum(x => x.Amount); // Amount is a decimal

在上面的语句中opaSum等于2.46914,而它应该是2.46913578(直接在MySQL上计算).opasForThisIp是类型的IQueryable<OutgoingPaymentAssembly>.

我需要所有Linq计算来处理8个小数位decimals.

有关如何解决此问题的任何想法?

编辑1:我发现var opasSum = Enumerable.Sum(opasForThisIp, opa => opa.Amount);产生了正确的结果,但问题仍然存在,为什么.Sum()要对结果进行舍入以及如何修复它?

编辑2:生成的SQL似乎有问题:

select cast(sum(outgoingpa0_.Amount) as DECIMAL(19,5)) as col_0_0_ 
from `OutgoingPaymentAssembly` outgoingpa0_ 
where outgoingpa0_.IncomingPayment_id=?p0 
and (outgoingpa0_.OutgoingPaymentTransaction_id is not null);
?p0 = 24 [Type: UInt64 (0)]
Run Code Online (Sandbox Code Playgroud)

编辑3:var opasSum = opasForThisIp.ToList().Sum(x => x.Amount);也产生正确的结果.

编辑4:转换的IQueryable<OutgoingPaymentAssembly>一个IList<OutgoingPaymentAssembly>由原始查询:var opasSum = opasForThisIp.Sum(x => x.Amount);上班.