title: Calculate average of an specific data that can be neutral
body: <p>I've searched about how Average lambda expression exactly works, but couldn't find.</p>
  
  <p>My problem is: I have some sentiment classification that is represented by diferent ways. For example, positive sentences are represented by a number > than 0. It could be from 1 to infinite (1, 2, 5, 100....) and for negative sentences the same way, but represented by a number &lt; than 0 (-1, -2.....) - I know that isn't the better way to represent that.</p>
  
  <p>And I need to calculate the positive occurrence average. Every answer from the questions on SOF I have found here, if I sum all values and divide by the total amount I will find the average. That's ok.</p>
  
  <p>For example, if I had 11 sentences and 10 are positive with value = 1, (sum = 10) and one sentence negative with value = -10 my average is going to be: 10-10/ 11 = 0.</p>
  
  <pre><code>var average = context.CatchDetails.Where(x =&gt; x.Monitoring.Client.Id == custmrid
              &amp;&amp; x.Data.published &gt;= datai2
              &amp;&amp; x.Data.published &lt;= dataf2
              &amp;&amp; x.Monitoring.id == monitoring).ToList().Average(y =&gt; y.Sentiment);
  </code></pre>
  
  <p>What I am doing is, taking all positive sentences (count) and dividing by the total sentences (count). Example:</p>
  
  <p>total count = 11<br>
  positive count = 10</p>
  
  <p>10 / 11 = 0.9090 that is 90,90% positive occurrences.</p>
  
  <pre><code>var positive = context.CatchDetails.Count(x =&gt; (x.Monitoring.Client.Id == custmrid)
              &amp;&amp; x.Data.published &gt;= datai2 &amp;&amp; x.Data.published &lt;= dataf2
              &amp;&amp; x.Monitoring.id == monitoring
              &amp;&amp; x.Sentiment &gt; 0);
  
  var total = context.CatchDetails.Count(x =&gt; (x.Monitoring.Client.Id == custmrid)
             &amp;&amp; x.Data.published &gt;= datai2 &amp;&amp; x.Data.published &lt;= dataf2
             &amp;&amp; x.Monitoring.id == monitoring);
  
  double mediaPositive = ((double)positive / (double)total);
  </code></pre>
  
  <p>Is there any better possibility for doing that? </p>
  
  <p>Searches:</p>
  
  <p><a href="https://stackoverflow.com/questions/32533555/how-to-get-words-average-length-using-lambda-expression">How to get words average length using Lambda Expression</a></p>
  
  <p><a href="https://stackoverflow.com/questions/30700904/getting-an-average-from-a-c-sharp-array/30700933#30700933">Getting an average from a C# array</a></p>