title: Entity Framework DateTIme Query
body: <p>I'm having an issue with a linq subquery return invalid data when adding in datetime checks as part of the <code>where</code> clause.</p>
  
  <p>This is the original query and it is returning 0; because the result set is <code>null</code></p>
  
  <pre><code>var subquery = 
      (from item in g
       from e in item.Entry
      where e.Type == 1 
         &amp;&amp; e.EntryType == 2
         &amp;&amp; item.StartDate &gt;= priorMonthStartOfDay
         &amp;&amp; item.EndDate &lt;= startOfDayQueryParam
     select e.Amount).Sum() ?? 0M;
  </code></pre>
  
  <p>I modified the query to see what the data was; here is that query and the resulting dataset.</p>
  
  <pre><code>var subquery = 
      (from item in g
       from e in item.Entry
      where e.Type == 1 
         &amp;&amp; e.EntryType == 2
     select new 
            {
                 Amount = e.Amount,
                 SD = item.StartDate,
                 ED = item.EndDate,
                 QD = priorMonthStartOfDay
            };
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/GQZ9D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GQZ9D.png" alt="no date restriction results"></a></p>
  
  <p>So then I added in the start date comparison and the results are below.  The <code>priorMonthStartOfDay</code> is a DateTime with a value of <code>12/1/2015 12:00:00 AM</code></p>
  
  <pre><code>var subquery = 
      (from item in g
       from e in item.Entry
      where e.Type == 1 
         &amp;&amp; e.EntryType == 2
         &amp;&amp; item.StartDate &gt;= priorMonthStartOfDay
     select new 
            {
                 Amount = e.Amount,
                 SD = item.StartDate,
                 ED = item.EndDate,
                 QD = priorMonthStartOfDay
            };
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/2XAL6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2XAL6.png" alt="start date restriction"></a></p>
  
  <p>Why is the date comparison not behaving as I would expected?  Given the value of <code>priorMonthStartOfDay</code>, I would expect the result set to be the same for the last two queries.  I'm guessing it has something to do with the time equal comparison because if I subtract a second from the <code>priorMonthStartOfDay</code> then the result sets match up again.</p>