Additional information: The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
also your if condition then needs to change as p1 and p2 will always have a value, it just may be zero, and they don't need to be nullable ints, unless this is used somewhere outside this chunk of code
Member
64 Points
153 Posts
sum in query
Jul 29, 2014 03:52 AM|Neda99|LINK
hi
i use this code for sum a field
but i get this error
mvc mvc5
Participant
1322 Points
451 Posts
Re: sum in query
Jul 29, 2014 06:39 AM|Dr. Acula|LINK
is this error occuring on your .Sum lines?
I think the problem is you can't sum a null value, try the null coalescing operator, so Sum uses zero where p1 or p2 is null
http://weblogs.asp.net/scottgu/the-new-c-null-coalescing-operator-and-using-it-with-linq
mvc mvc5
Participant
1322 Points
451 Posts
Re: sum in query
Jul 29, 2014 06:43 AM|Dr. Acula|LINK
also your if condition then needs to change as p1 and p2 will always have a value, it just may be zero, and they don't need to be nullable ints, unless this is used somewhere outside this chunk of code
mvc mvc5
Member
200 Points
52 Posts
Re: sum in query
Jul 29, 2014 08:13 AM|sandipnehate|LINK
Hi,
Assuming that P1 and P2 are nullable
If this does not solve your problem can you post a little bit of your code what is the DB columns and its types
mvc mvc5
Sandip Nehate