title: Entity Framework Error - The cast to value type 'System.Int64' failed because the materialized value is null
body: <p>The below method fails when Execting ToListAsync() with the following error</p>
  
  <pre><code>The cast to value type 'System.Int64' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type. 
  </code></pre>
  
  <p>--</p>
  
  <pre><code> private IQueryable&lt;MaterialSearchResult&gt; MaterialSearchQuery(string searchTerm)
              {    
                  return from m in this.context.GB_Material
                         from v in this.context.WF_VideoVersion
                             .Where(
                                 v =&gt;
                                     v.MaterialID == m.MaterialID)
                             .DefaultIfEmpty()
                         select new MaterialSearchResult
                         {
                             MaterialId = v.MaterialID,
                             MaterialName = v.GB_Material.MaterialName,
                             MaterialTitle = v.GB_Material.MaterialTitle,
                             CreatedDate = v.GB_Material.CreatedDate,
                             NearestTxDate = v.NearestTXDate,
                             Channel = v.ScheduleName
                         };
      }
  </code></pre>
  
  <p>Calling method</p>
  
  <pre><code>public Task&lt;List&lt;MaterialSearchResult&gt;&gt; SearchMaterials(string searchTerm, string sort, string direction, int pageSize = 20, int skip = 0)
      {
          var data = MaterialSearchQuery(searchTerm)
              .SortMaterials(sort,direction)
              .Skip(skip)
              .Take(pageSize);
  
          return data.ToListAsync();
      }
  
  public class MaterialSearchResult
  {
      public long MaterialId { get; set; }
      public string MaterialName { get; set; }
      public string MaterialTitle { get; set; }
      public DateTime? NearestTxDate { get; set; }
      public string Channel { get; set; }
      public DateTime CreatedDate { get; set; }
  }
  
   public class GB_Material()
      {
         public long MaterialID { get; set; }
      public string MaterialName { get; set; }
      public string MaterialTitle { get; set; }
      public System.DateTime CreatedDate { get; set; }
  
     public virtual ICollection&lt;WF_VideoVersion&gt; WF_VideoVersion { get; set; }
      }
  
       public class WF_VideoVersion()
      {
           public long VideoVersionID { get; set; }
           public long MaterialID { get; set; }
           public Nullable&lt;System.DateTime&gt; NearestTXDate { get; set; }
           public string ScheduleName { get; set; }
  
           public virtual GB_Material GB_Material { get; set; }
  
      }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I'm also getting the same error on the NearestTxDate property</p>
  
  <pre><code>The cast to value type 'System.DateTime' failed because the materialized value is null. 
  </code></pre>