How would one use the MSSQL 2016 AT TIMEZONE
feature from Entity Framework?
In other words, how to generate
SELECT MyTimestamp AT TIME ZONE 'Central European Standard Time' FROM MyTable
by LINQ in entity framework ? Is it even supported yet ? Can one extend Entity Framework manually for this feature ?
There is always the option of creating a database-view with a time-zone column, but ideally I would like to avoid this extra view.
AT TIMEZONE
is a blessing for T-SQL programmers who couldn't do this from application code before, at all. In .NET, there's excellent time zone support which sort of obviates the need for this, since you can always useTimeZoneInfo.ConvertTime
to convert aDateTime
orDateTimeOffset
to any desired time zone. I'm not saying there's no need for this, but if you can do any sort of processing on the result, there's at least a workaround. – BlungerTimeZoneInfo.ConvertTime
or (more likely) implement a new method inSqlFunctions
. I don't think you can do this as a client of EF, though. You could write your own scalar-valued function forAT TIMEZONE
and call that, but that's lame and inefficient as the optimizer hates scalar-valued functions. – BlungerCREATE FUNCTION dbo.AtTimeZone(@d DATETIMEOFFSET, @TimeZone SYSNAME) RETURNS TABLE AS RETURN SELECT @d AT TIME ZONE @TimeZone AS AtTimeZone;
EF 5+ has TVF support, though I don't know if it canCROSS APPLY
properly. Of course, a view is simpler (as you proposed), but only works for one particular query. Last but not least, you can of course open an issue. – Blunger