简体   繁体   English

linq-to-sql分组与时区

[英]linq-to-sql grouping with timezone

I have a linq-to-sql query that groups results by DateTime, something like this: 我有一个linq-to-sql查询,它按DateTime对结果进行分组,如下所示:

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. 问题是这个组按实际日期在数据库中以UTC格式存储。 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. 因此,例如,如果他在PST时区,按“UTC减去8小时”或其他任何偏移分组。

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. 如果你还是得到了所有的信息,我会坦率地把它带回客户端并在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. 这样你就可以专注于使用一个平台的日期和时间API - 当涉及时区时,使用一个API是很困难的,更不用说两个了。

(As an aside and a plug, it also means you can use whatever .NET API you like for this - such as Noda Time ;) (作为一个插件,它也意味着您可以使用任何您喜欢的.NET API - 例如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. 因此,我将在分组之前进行所有可以进行的过滤和投影,然后使用AsEnumerable()强制执行本地的其余操作。

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). 不要忘记你几乎肯定不能只增加或减少8小时,除非用户真正处于不使用夏令时的时区(并且在历史上没有)。 Are you also sure that it's appropriate to apply the same time zone to all the data? 您是否也确定将相同的时区应用于所有数据是否合适? (It may well - just checking.) (很可能 - 只是检查。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM