简体   繁体   English

不同的日期时间格式在 EF Core 和 LINQ 中返回错误的结果集。 DateTime.ParseExact 不起作用

[英]Different datetime format returns wrong result set in EF Core and LINQ. DateTime.ParseExact does not work

I have an ASP.NET Core MVC application with the culture set to en-GB using a date time format of dd/mm/yyyy .我有一个 ASP.NET Core MVC 应用程序,其文化设置为en-GB ,使用日期时间格式dd/mm/yyyy

I am using:我在用:

  • .NET Core 2.1 .NET 核心2.1
  • EF Core 2.2 EF 核心 2.2
  • SQL Server 2019 (v15) SQL 服务器 2019 (v15)
  • The date column is of type datetime2日期列的类型为datetime2

However for one of my queries I need the date to be of format yyyy-mm-dd when querying through EF Core.但是,对于我的一个查询,在通过 EF Core 查询时,我需要日期格式为yyyy-mm-dd

var xyx = db.zyz
            .Where(x => x.date >= startdate && x.date<= enddate)
            ......

The above query fails to return the result set even if there is a record in table zyz in accordance to the condition.上面的查询即使按照条件在zyz表中有一条记录,也无法返回结果集。

This I think is happening only when startdate and enddate are the same.我认为只有当 startdate 和 enddate 相同时才会发生这种情况。

Eg let's say zyz has a record for date 2020-04-19例如,假设 zyz 的记录日期为2020-04-19

Query询问

   var dt = DateTime.ParseExact("20200419","yyyyMMdd",CultureInfo.InvariantCulture);

    var xyx = db.zyz
                .Where(x=>x.date >= dt && x.date<= dt)
                ......

This fails to fetch any rows.这无法获取任何行。 It returns 0 rows.它返回 0 行。

I think this is because of the date time format.我认为这是因为日期时间格式。

The date field in zyz in the db is of the format 2020-19-04 00:00:00.0000000 db 中 zyz 中的日期字段格式2020-19-04 00:00:00.0000000

Two questions come to mind:想到两个问题:

  • Is this because of the datetime format that I am not getting a result?这是因为我没有得到结果的日期时间格式吗? Does EF Core not take care of the formatting? EF Core 不处理格式吗?
  • When I use ParseExact and different means to parse the date to a format I want.当我使用ParseExact和其他方法将日期解析为我想要的格式时。 It does not work.这没用。 I always get the date in the format of dd/mm/yyyy .我总是以dd/mm/yyyy格式获取日期。

What I have tried, to parse the date:我尝试过解析日期:

var dt = DateTime.ParseExact("20200415","yyyyMMdd",CultureInfo.InvariantCulture);

DateTime theTime = DateTime.ParseExact("20200415",
                                        "yyyyMMdd",
                                        CultureInfo.InvariantCulture,
                                        DateTimeStyles.None);

var dts = DateTime.ParseExact("20200415", "yyyyMMdd",new CultureInfo("en-ZA"));

The above queries always return date of format 19/04/2020 00:00:00上述查询始终返回格式为19/04/2020 00:00:00的日期

As I understand since datetime is just a datatype, shouldn't the query be independent of the datetime format and return data irrespective of the datetime format?据我了解,由于 datetime 只是一种数据类型,因此查询不应该独立于 datetime 格式并返回数据而不考虑 datetime 格式吗?

Hi guys the query itself was wrong.大家好,查询本身是错误的。

What was wrong:什么问题:

In case of same start date and end date:如果开始日期和结束日期相同:

var xyx = db.zyz
            .Where(x => x.date >= startdate && x.date<= enddate)
            ......

I was sending:我正在发送:

2020-04-19 00:00:00 for both 2020-04-19 00:00:00 双方

I had to send:我不得不发送:

2020-04-19 00:00:00 - for start date 2020-04-19 11:59:00 - for end date 2020-04-19 00:00:00 - 开始日期2020-04-19 11:59:00 - 结束日期

As pointed out by Ivan Stoev and Stefano Balzarotti in the comments.正如Ivan StoevStefano Balzarotti在评论中指出的那样。

Wrote this answer as it makes it clear that datetime is a type and does not have a format as pointed out by Jon Skeet in the comments.写了这个答案,因为它清楚地表明 datetime 是一种类型,并且没有Jon Skeet在评论中指出的格式。

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

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