linq to sql - Linq2EF: Return DateTime with a TimeZone Offset -
our database has times stored utc, , know user's current timezone, want return relative that. want incorporate offset in linq projection so:
var users = u in this.context.users select new userwithcorrecteddate { id = u.id, firstname = u.firstname, lastname = u.lastname, registrationdate = u.registrationdate.value.addhours(-5) };
of course, linq2ef cannot convert "addhours
" canonical function. there way this?
update:
another thought, if timezone offset stored in database column, there way have db perform calculation (date + offset)?
linq entities supports sqlfunctions. if know offset number of hours this:
var users = u in this.context.users select new userwithcorrecteddate { id = u.id, firstname = u.firstname, lastname = u.lastname, registrationdate = sqlfunctions.dateadd("hour", -5, u.registrationdate.value) };
if want more precise using timezone function, (this bit of workaround):
public static datetime utctonewyorktime(datetime utcdatetime) { timezoneinfo tzi = timezoneinfo.findsystemtimezonebyid("eastern standard time"); datetime converted = timezoneinfo.converttimefromutc(utcdatetime, tzi); return converted; }
and in poco userwithcorrecteddate object, have property timezoned date ie.
public class userwithcorrectdate { public datetime utcdate {get;set;} public datetime nydate { { return utilities.utctonewyorktime(this.utcdate); } } }
Comments
Post a Comment