title: LINQ, handle Null in Model
body: <p>Im trying to pass AverageRating to my view. AverageRating is the result of querying items in an Icollection of Review Model. Review Model has a property of Rating. However i get this message:</p>
  
  <blockquote>
    <blockquote>
      <p>System.ArgumentNullException </p>
    </blockquote>
  </blockquote>
  
  <p>This happens whenever the get is performed, which is understandable. However how do i best handle null exceptions in my model or elsewhere when my code looks like the following:</p>
  
  <pre><code>public class MyModel
  {
  
          //Querying this navigation property
          public ICollection&lt;Review&gt; Reviews { get; set; }
  
          public double? AverageRating
          {
          get
          { 
              //check that this is not null / handle null
              return Math.Round(Reviews.Average(c =&gt; c.Rating), 1);
          }
  
          }
  
  }
  </code></pre>
  
  
  
  
  
  <pre><code>public class Review
  {
      [Key]
      public int ReviewID { get; set; }
      public int Rating { get; set; }
      public string Comment { get; set; }
      public int CoachID { get; set; }
      public int? StudentID { get; set; }
  
  
      public Coach Coach { get; set; }
      public Student Student { get; set; }
  }
  </code></pre>