Csla.Linq Sum bug

Csla.Linq Sum bug

Old forum URL: forums.lhotka.net/forums/t/8403.aspx


jureleskovec posted on Monday, January 25, 2010

Sum doesn't work for the 'decimal?' (nullable decimal) type. It throws error that it can't convert decimal? to Int32:

decimal? sum = invoiceItems.Sum(ii => i.Ammount);  // Ammount is 'decimal?'

-- Jure

richardb replied on Monday, January 25, 2010

You might be able to use the GetValueOrDefault property on the decimal type, which will return zero if it is null.

AaronErickson replied on Monday, January 25, 2010

In the short term, the best thing you can probably do is use the static alternative:

var sum = Enumerable.Sum(invoiceItems, i => i.Amount);

There is an issue that affected certain older versions of the framework that was picking the wrong overload of Enumerable in the L2C logic.  Most of those issues have been fixed in various releases up to 3.8.  Do you have a version number of the framework you are working with?

jureleskovec replied on Monday, January 25, 2010

Oh, yes. The version is 3.6.2.

AaronErickson replied on Monday, January 25, 2010

I would be interested if you end up with the same issue with the current version (3.8.x) - otherwise, hopefully the above workaround will work for you.

Copyright (c) Marimer LLC