title: Handle null in LINQ sum expression
body: <p>I am using a LINQ query to find sum of a column and there is a slight chance that the value might be null in few cases </p>
  
  <p>The query I am using now is </p>
  
  <pre><code>int score = dbContext.domainmaps.Where(p =&gt; p.SchoolId == schoolid).Sum(v =&gt; v.domainstatement.Score ?? 0);
  </code></pre>
  
  <p>where <code>domainstatement</code> can be null and score also can be null </p>
  
  <p>Now after executing this query, I am getting the error</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>So how can I handle null exceptions effectively and return sum as an INT value?</p>