简体   繁体   English

linq 查询与时区 groupby

[英]linq query with time zones groupby

I have a linq-to-sql query that works by grouping the data it retrieve in days for a particular month:我有一个 linq-to-sql 查询,它通过对特定月份以天为单位检索的数据进行分组来工作:

var Output = from c ....
             where .... // this is the month parameter
             group c by c.TheTime.Date into daygroups
             select new MyModel(){

                Prop1 = (from x in daygroups
                         where....
                         select x.ID).Count()
}.ToList();

The problem is that this groups by datetime in terms of server time.问题在于,这根据服务器时间按日期时间分组。 I'd like to group them by interval of times so that if we're looking at the result with California time, we're really looking at a list of days that starts at midnight PST.我想按时间间隔对它们进行分组,这样如果我们查看加利福尼亚时间的结果,我们实际上是在查看从太平洋标准时间午夜开始的日期列表。

The query returns a count.查询返回一个计数。 The solution I've figured out for now would be to return all the raw data between the beginning and the end of the month in a certain timezone, and then rearranging the raw data in days with the correct timezone, and only then do the count.我现在想出的解决方案是在某个时区返回月初和月底之间的所有原始数据,然后用正确的时区在天数内重新排列原始数据,然后才进行计数.

Is there a better way to do this?有一个更好的方法吗?

Try grouping by c.TheTime.Date.AddHours(-8) .尝试按c.TheTime.Date.AddHours(-8)分组。
However, I'm not sure whether that will work correctly.但是,我不确定这是否会正常工作。

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

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