title: Linq with sum with nullable int
body: <p>I am trying to compute a sum of nullable int using Linq. My query looks like this:</p>
  
  <pre><code>int? surveysPromotersFromChatsGroup = (from surveyResult in surveyResultsFromChats
                                              where surveyResult.FirstScoreNPS.HasValue &amp;&amp; surveyResult.GroupID == gr &amp;&amp; surveyResult.FirstScoreNPS &gt;= 9
                                              select surveyResult.FirstScoreNPS).Sum();
  </code></pre>
  
  <p>FirstScoreNPs field is a nullable int. My problem is that the sum always return null no matter what. I can't see the logic behind this. I checked and I know for certain that some entities do have values in that field and the sum method should sum up those values.</p>
  
  <p>Another approach that I tried is like this:</p>
  
  <pre><code>int surveysPromotersFromChatsGroup = (from surveyResult in surveyResultsFromChats
                                        where surveyResult.FirstScoreNPS.HasValue &amp;&amp; surveyResult.GroupID == gr &amp;&amp; surveyResult.FirstScoreNPS &gt;= 9
                                        select surveyResult.FirstScoreNPS).Sum(score =&gt; score.Value);
  </code></pre>
  
  <p>But I get an exception "linq the null value cannot be assigned to a member with type".</p>
  
  <p>Isn't there a way to sum up the non null values from that field using linq?</p>
  
  <p>The surveyResultsFromChat is actually from another query:</p>
  
  <pre><code>                var surveyResultsFromChats = from surveyResult in db.SurveyResults
                                      join survey in db.Surveys on surveyResult.SurveyID equals survey.SurveyID
                                      join userGroup in db.UserGroupMaps on surveyResult.UserID equals userGroup.UserID
                                      where surveyResult.CreatedDate &gt;= startDate &amp;&amp; surveyResult.CreatedDate &lt;= endDate &amp;&amp; surveyResult.UserID.HasValue &amp;&amp; surveyResult.ChatSessionID.HasValue
                                      select new { surveyResult.ResultID, survey.BrandID, userGroup.GroupID, surveyResult.CompletedDate, surveyResult.FirstScoreNPS };
  </code></pre>
  
  <p>FirstScoreNPS in my model is an Nullable int.</p>