简体   繁体   English

包含 UTC 时区转换的 Linq 查询 where 子句

[英]Linq query where clause to include the UTC Timezone conversion

I have a DB in Azure SQL Server where I have to compare the Date field against the current Eastern zone time, the SQL query where clause is like below我在 Azure SQL Server 中有一个数据库,我必须将日期字段与当前东部地区时间进行比较,SQL 查询 where 子句如下所示

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

Where current datetime in the Azure SQL DB is converted in to the EST time. Azure SQL DB 中的当前日期时间转换为 EST 时间的位置。 I am not sure how I can convert above query to the linq query so I can use it in the C# like below我不确定如何将上述查询转换为 linq 查询,以便我可以在 C# 中使用它,如下所示

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

but this doent work and errors但这没有工作和错误

My bad as I was do the UTC conversion directly in the Query.我的坏处是直接在查询中进行 UTC 转换。 I tried like below我试过如下

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

and used the todayDt in the where clause并在 where 子句中使用了todayDt

where mo.OrderDate.Date == todayDt.Date

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

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