I have a table with Time
column of type DATETIME
and I want to craft a piece of Linq-To-Sql code that does this:
SELECT Whatever FROM ThatTable WHERE Time > DATEADD(hour, -3, GETUTCDATE())
and the key is I want SQL Server time used, so I used the trick from here with a [Function(Name="GetUTCDate", IsComposable=true)]
- attributed method in my DataContext
.
My code goes like this:
context.GetTable<ThatTable>().Where(
item => item.Time > context.GetServerUTCDate().AddHours(-3) );
and when I output the emitted SQL query I see that it has this predicate:
WHERE [t0].[Time] > DATEADD(ms,
(CONVERT(BigInt,@p0 * 3600000)) % 86400000,
DATEADD(day, (CONVERT(BigInt, @p0 * 3600000)) / 86400000,
GetUTCDate()))
Here it actually makes use of GetUTCDate()
but clearly it failed to make use of hour
and instead emitted some magic for comparing number of hours to milliseconds and working with those.
Can I make it use hour
instead of magic?