title: Handling null results with the LINQ Average() method
body: <p>I am new to <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query" rel="noreferrer">LINQ</a> and am trying to create some data points from a table to graph. The three fields of importance in this table are the id, the time and the value. I am writing a query to get the average value over a set time for a chosen id. The LINQ I have written follows:</p>
  
  <pre><code>var value = (from t in _table
               where t.Id == id
                   &amp;&amp; t.Time &gt;= intervalStartTime
                   &amp;&amp; t.Time &lt;= intervalEndTime
               select t.Value).Average();
  </code></pre>
  
  <p>However this crashes at runtime with:</p>
  
  <blockquote>
    <p>"The null value cannot be assigned to
    a member with type System.Decimal
    which is a non-nullable value type.."</p>
  </blockquote>
  
  <p>At certain intervals there is no data so the SQL LINQ generates returns null, which I would liked to be COALESCED to 0 but instead crashes the application. Is there a way to write this LINQ query to be able to handle this properly?</p>
  
  <p>The table definition to make things clearer:</p>
  
  <pre><code>[Serializable]
  [Table(Name = "ExampleTable")]
  public class ExampleTable
  {
      [Column(Name = "Id")]
      public int Id { get; set; }
  
      [Column(Name = "Time")]
      public DateTime Time { get; set; }
  
      [Column(Name = "Value")]
      public int Value{ get; set; }
  }
  </code></pre>