UTC为本地时间使用LINQ到实体实体、时间、UTC、LINQ

2023-09-03 08:10:36 作者:跪下喊爷!

我需要翻译在LINQ到实体查询从UTC本地时间字段。但它不能识别方法的System.DateTime ToLocalTime',我是打算用。

I need to translate a field from UTC to local time in a LINQ to Entities query. But it does not recognize the method 'System.DateTime ToLocalTime' that I was intended to use.

我的查询是这样:

对于不存在于同一天的任何其它请求在本地选择请求尚未解决的。具有成IncommingDateTime存储在UTC帐户,这是查询

Select requests for which doesn't exist any other request in the same day in local not yet resolved. Having into account that IncommingDateTime is stored in UTC, this is the query.

from r in Requests
where Request.Count(x => x.IncommingDateTime.ToLocalTime().Date == r.IncommingDateTime.ToLocalTime().Date 
&& x.Resolved == false ) = 0
select r

我会AP preciate任何帮助。

I'll appreciate any help.

推荐答案

你所期望的时区使用?唯一的一个真正的数据库默认情况下知道的很可能是自己的。

What time zone would you expect that to use? The only one the database really knows about by default would probably be its own.

注意 DateTime.Day 只重presents月的一天 - ?你想6月10日和7月10日算作当日

Note that DateTime.Day only represents the day of the month - do you want June 10th and July 10th to count as the same day?

如果你可以推断出你在做什么时区很感兴趣,这可能意味着一个潜在的解决方案 - 虽然我不喜欢保证。这是可能的,这是对这些情况下的实际上的是有意义的存储在本地的日期和时间,以及时区,而不是UTC日期/时间之一。 (这是适当的,如果本地日期/时间实际上比瞬间重新presented更重要的是,相对于其他的值。)

If you can work out what time zone you're really interested in, that may suggest a potential solution - although I wouldn't like to guarantee it. It's possible that this is one of those cases where actually it makes sense to store the local date and time, as well as the time zone, rather than the UTC date/time. (This is appropriate if the local date/time is actually more important than the instant represented, compared to other values.)