Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The fundamental difference between the two cases is that:</p> <ul> <li>in the first case, LINQ2SQL translates the <code>Where</code> condition to SQL and passes it to the DB to do the actual filtering</li> <li>in the second case all records are retrieved from the database and the filtering is done on an in-memory list of objects</li> </ul> <p>The result difference may for example be explained by a truncation which might take place in SQL if the <code>Datum</code> columns are defined as <code>date</code> instead of <code>datetime</code>.</p> <p><strong><em>I managed to reproduce the problem like so:</em></strong></p> <p>I have a table defined like this:</p> <pre><code>CREATE TABLE [Event] ( [Id] int identity primary key, [EventDate] date ) </code></pre> <p>I entered the following data in the table:</p> <pre><code>Id EventDate -- --------- 1 1/17/2012 2 1/18/2012 </code></pre> <p>In C#, my date limits are defined like so:</p> <pre><code>var startDate = new DateTime(2012, 1, 17); var endDate = new DateTime(2012, 1, 18).AddMinutes(10); </code></pre> <p>Having this set up, I now do:</p> <pre><code>var events1 = dc.Events .Where(d =&gt; d.EventDate &gt; startDate &amp;&amp; d.EventDate &lt; endDate) .ToList(); foreach (var @event in events1) Console.WriteLine(@event.EventDate); </code></pre> <p>which returns <strong>only the first row (EventDate = 1/17/2012)</strong></p> <p>And I do:</p> <pre><code>var events2 = dc.Events.ToList(); foreach (var @event in events2 .Where(d =&gt; d.EventDate &gt; startDate &amp;&amp; d.EventDate &lt; endDate)) Console.WriteLine(@event.EventDate); </code></pre> <p>which displays <strong>both records</strong>.</p> <p>The difference is that in the first case, L2S translated the upper date limit from <code>datetime</code> to <code>date</code>, because that's how I defined the <code>[EventDate]</code> column. This loses the time part, and <code>'2012-01-18 00:10'</code> becomes <code>'2012-01-18'</code>. When comparing with '&lt;' (strictly less than), of course the 2012-01-18 record is filtered out by SQL.</p> <p>In the other case I have the dates as they are in SQL, but this time they are filtered in-memory against the <code>endDate</code> value which is defined as a <code>DateTime</code>, thus holds time information and the 'strictly less than' condition becomes true.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload