Note that there are some explanatory texts on larger screens.

plurals
  1. POUse Linq to SQL to search only time portions of a date in SQL 2008
    primarykey
    data
    text
    <p>I want to be able to search on a DateTime field not only by a date range, but also by a time range. For example, "Get me all records in the month of May created between 1pm and 3pm". The SQL I'm looking to generated is:</p> <pre><code>WHERE CreatedOn BETWEEN '05/01/2010' AND '06/01/2010' AND CONVERT(time(4), CreatedOn) BETWEEN '01:00 PM' AND '03:00 PM' </code></pre> <p>I can't seem to figure out how to get a lambda expression to get me there. Anybody got any clue? I've got System.Linq.Dynamic in my project. Similar to the way you can pass a string as an OrderBy clause, I thought I might be able to do the same with the Where method as follows::</p> <pre><code>results = results.Where("CONVERT(time(4), OccurredOn) BETWEEN '{0}' AND '{1}'", Criteria.OffenseTimeStart, Criteria.OffenseTimeEnd); </code></pre> <p>But that throws an exception in System.Linq.Dynamic. Anybody got any pointers?</p> <p>THANKS FOR THE HELP! Here is my working code excerpt. I exposed my time criteria properties as TimeSpans:</p> <pre><code> if (Criteria.OffenseDateStart.HasValue) results = results.Where(o =&gt; o.OccurredOn &gt;= Criteria.OffenseDateStart); if (Criteria.OffenseDateEnd.HasValue) results = results.Where(o =&gt; o.OccurredOn &lt;= Criteria.OffenseDateEnd); if (Criteria.OffenseTimeStart.HasValue) results = results.Where(o =&gt; o.OccurredOn.TimeOfDay &gt;= Criteria.OffenseTimeStart); if (Criteria.OffenseTimeEnd.HasValue) results = results.Where(o =&gt; o.OccurredOn.TimeOfDay &lt;= Criteria.OffenseTimeEnd); </code></pre>
    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.
 

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