I have to sum all the values in one object. The values are retrieved from a database and stored in a List<T>. If one of the value is equal to -1 I want to use 0 in the sum instead.
Here is the code that I use to perform the task:
lprod_projectionActualvalue = lprod_monthlyReport.Sum(m => new
{
Monthly_ActualValue = (m.Monthly_ActualValue != -1) ?
m.Monthly_ActualValue : 0F
});
Monthly_ActualValue is of type float?. The compiler gives me an error stating that I cannot convert anonymous type to float?. That is quite strange because I use the same code in the Select clause without getting any error.
What could be the problem?
Thanks
Francesco
You’re creating a new anonymous type using the
new {}keyword and brackets, which is never going to be convertible to a float. If you want the sum to be of a float, remove the anonymous type declaration and just specify the expression producing the float, like so: