title: Linq to Sql .Except not working if null in C#
body: <p>I have a simple query.</p>
  
  <p>It checks for a list of rooms - <code>prebookedRooms</code></p>
  
  <p>It then checks for another list of rooms, but uses the <code>.Except</code> operator, to remove any <code>prebookedRooms</code> from the subsequent list:</p>
  
  <pre><code>        // Get list of rooms already booked
          var prebookedRooms = dbt.Rooms
              .Where(room =&gt; room.Rentals.Any(rental =&gt;
                  (dteFrom &gt;= rental.check_in &amp;&amp; dteFrom &lt; rental.check_out)));
  
          // Get list of rooms
          var rooms = dbt.Rooms.Where(r =&gt; r.h_id == AccID)
              .Except(prebookedRooms)
              .GroupBy(p =&gt; p.RoomTypes).Select(g =&gt; new RatesViewModel
              {
                  TypeName = g.Key.t_name,
                  TypeID = g.Key.t_id,
                  TypeCount = g.Count()
              })
              .ToList();
  </code></pre>
  
  <p>This works fine - with <code>rooms</code> returning a list of rooms, with <code>prebookedRooms</code> excluded.</p>
  
  <p>However, if <code>prebookedRooms</code> doesn't return any records (ie. Empty "Enumeration yielded no results") - then I get an error when executing the second query (var rooms = ...):</p>
  
  <p><code>The cast to value type 'Int64' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.</code></p>
  
  <p>Is there anyway of checking if prebookedRooms is empty, within the second query, so I can avoid this error?</p>
  
  <p><strong>prebookedRooms model:</strong></p>
  
  <pre><code>public class Room
  {
      [Key]
      public long room_id { get; set; }
      public long hotel_id { get; set; }
      public long type_id { get; set; }
  }
  </code></pre>
  
  <p>The error as it appears in VS is:
  <img src="https://i.stack.imgur.com/VYjQJ.png" alt="enter image description here"></p>
  
  <p>Thank you,</p>
  
  <p>Mark</p>