【问题标题】:Linq query where clause to include the UTC Timezone conversion包含 UTC 时区转换的 Linq 查询 where 子句
【发布时间】:2021-12-15 22:07:05
【问题描述】:

我在 Azure SQL Server 中有一个数据库,我必须将 Date 字段与当前东部地区时间进行比较,SQL 查询 where 子句如下所示

where CAST(getdate() AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time' as date) = mo.orderdate

Azure SQL DB 中的当前日期时间转换为 EST 时间。我不确定如何将上述查询转换为 linq 查询,以便可以在 C# 中使用它,如下所示

 where mo.orderdate ==  CAST(getdate() AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time' as date) 

但这确实有效并且错误

【问题讨论】:

  • CAST(getdate() AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time' as date) 可能比CAST(SYSUTCDATETIME() AT TIME ZONE 'Eastern Standard Time' as date) 更好

标签: c# sql-server linq azure-sql-database azure-sql-server


【解决方案1】:

因为我直接在查询中进行 UTC 转换,所以我很糟糕。我试过如下

        var timeUtc = DateTime.UtcNow;
        var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
        var todayDt = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);

并在 where 子句中使用了todayDt

where mo.OrderDate.Date == todayDt.Date

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 2023-03-21
    • 2020-12-31
    相关资源
    最近更新 更多