I have a linq-to-sql query that groups results by DateTime, something like this:
var TheQuery = from ....
where ....
group a by a.TheDateTime.Date in TheGroups
The problem is that this groups by actual dates that are stored in UTC in the DB. Let’s say that I want to group by dates based on the user’s timezone. So for instance, if he’s in the PST timezone, group by “UTC minus 8 hours”, or whatever other offset.
Thanks for your suggestions.
If you’re getting all the information back anyway, I’d frankly bring it back to the client and do the grouping in LINQ to Objects. That way you can concentrate on using one platform’s date and time API – it’s hard enough using one API correctly when it comes to time zones, let alone two.
(As an aside and a plug, it also means you can use whatever .NET API you like for this – such as Noda Time 😉
So I’d put all the filtering and projection you can do before the grouping, then use
AsEnumerable()to force the rest of the execution to occur locally.Don’t forget that you almost certainly can’t just add or subtract 8 hours, unless the user is really in a time zone which doesn’t use daylight saving time (and hasn’t historically). Are you also sure that it’s appropriate to apply the same time zone to all the data? (It may well – just checking.)