The cast to value type ‘Int32’ failed because the materialized value is null

I have the following code. I’m getting error:

“The cast to value type ‘Int32’ failed because the materialized value is null. Either the result type’s generic parameter or the query must use a nullable type.”

when CreditHistory table has no records.

var creditsSum = (from u in context.User
                  join ch in context.CreditHistory on u.ID equals ch.UserID                                        
                  where u.ID == userID
                  select ch.Amount).Sum();

How can I modify the query to accept null values?

8 Answers
8

Leave a Comment