title: "The cast to value type 'Boolean' failed because the materialized value is null" when nothing is null
body: <p>I have this line</p>
  
  <pre><code>if (EF.ctx.Searches.Any(x =&gt; x.SearchTerm == _searchTerm &amp;&amp; x.IpAddress == ip &amp;&amp; x.Time &gt; dtRecent))
  </code></pre>
  
  <p>throwing this error randomly (rarely, non-reproducible)</p>
  
  <blockquote>
    <p>The cast to value type 'Boolean' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.</p>
  </blockquote>
  
  <p>In the database, only IpAddress is nullable (but so far there is no record with null IpAddress).<br>
  Everytime the error was thrown, none of the variables were null.</p>
  
  <p>The closest similar question is <a href="https://stackoverflow.com/questions/25246178/queryable-any-returning-null">Queryable.Any() returning null?</a><br>
  But this error still doesn't make sense to me.</p>
  
  <p>I am really curious, what on earth can be <strong>null</strong> in that line?</p>
  
  <p>Stacktrace:</p>
  
  <blockquote>
    <p>at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader<code>1.GetValue(DbDataReader reader, Int32 ordinal)
        at lambda_method(Closure , Shaper )
        at System.Data.Common.Internal.Materialization.Coordinator</code>1.ReadNextElement(Shaper shaper)
        at System.Data.Common.Internal.Materialization.Shaper<code>1.SimpleEnumerator.MoveNext()
        at System.Linq.Enumerable.Single[TSource](IEnumerable</code>1 source)
        at System.Data.Objects.ELinq.ObjectQueryProvider.b__3[TResult](IEnumerable<code>1 sequence)
        at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable</code>1 query, Expression queryRoot)
        at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
        at System.Linq.Queryable.Any[TSource](IQueryable<code>1 source, Expression</code>1 predicate)
        at [my code, the line above]</p>
  </blockquote>