title: Checking null in LinQ
body: <p>This is my query:</p>
  
  <pre><code>var entityMerchantVisit =
       from e in context.MerchantCustomerVisit
       where e.CustomerId == currentCustGuid
       group e by  
             new { e.Merchant.Locations.FirstOrDefault().CityId } into mcvGroup
       orderby mcvGroup.Count() descending
       select mcvGroup;
  </code></pre>
  
  <p>I'm getting error </p>
  
  <blockquote>
    <p>"The cast to value type 'Int32' failed because the materialized value is null" when <strong>e.Merchant.Locations.FirstOrDefault().CityId</strong> is null.</p>
  </blockquote>
  
  <p>How do I check if it's null before hand. I would like to assign it as <code>(int) 0</code> , if it is null.</p>