title: How to translate TimeSpan calculations to SQL via LINQ
body: <p>I'm having an entity with a <code>TimeSpan</code> property (<code>Start</code>) and an <code>int</code> (<code>Duration</code> in <em>minutes</em>) property.</p>
  
  <p>I want to perform a linq query that will fetch all the rows that apply to the following logic:</p>
  
  <p><code>Start &lt;= DateTime.Now &lt;= Start + Duration</code></p>
  
  <p>My LINQ</p>
  
  <pre class="lang-cs prettyprint-override"><code>from n in _context.Notifications
  where n.Start &lt;= DateTime.Now.TimeOfDay &amp;&amp; DateTime.Now.TimeOfDay &lt;= n.Start.Add(new TimeSpan(0, n.Duration, 0))
  select n;      
  </code></pre>
  
  <p>translates into (as taken from the SQL Server Profiler)</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT [n].[Id], [n].[Active], [n].[Created], [n].[Day], [n].[Duration], [n].[Name], [n].[Start], [n].[Type]
  FROM [Notifications] AS [n]
  WHERE [n].[Start] &lt;= CAST(GETDATE() AS time)
  </code></pre>
  
  <p>The warning that I'm getting from the EntityFramework Core is</p>
  
  <blockquote>
    <p>Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'where 
    (DateTime.Now.TimeOfDay &lt;= [n].Start.Add(new TimeSpan(0, [n].Duration, 0)))' >could not be translated and will be evaluated locally.</p>
  </blockquote>
  
  <p>The SQL query that I want is this</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT [n].[Id], [n].[Active], [n].[Created], [n].[Day], [n].[Duration], [n].[Name], [n].[Start], [n].[Type]
  FROM [Notifications] AS [n]
  WHERE CAST(GETDATE() AS TIME) BETWEEN [n].[Start] AND DATEADD(MINUTE, [n].[Duration], [n].[Start])
  </code></pre>