Skip to content

Problems with date format on queries #23561

@arthuraugsten

Description

@arthuraugsten

File a bug

I'm facing this issue and I didn't find a solution for this case. I'm developing a system with .NET 5, EF Core 5.0 and SQL Server. There's an entity called Post with a property named SubmittedDate with Datetime? type. On database it's stored with time, but I have a feature that need to filter all Posts from a specific date (Ex.: 2020/12/2), I'm filtering using the following code, but EF Core pass the parameters to query in a wrong order. It change the order of day and month. I'm using correct parameters on EF.Functions.DateFromParts(...).

Are there anything about it? I didn't find nothing and doesn't have a solution. May you help me?

Include your code

var date = new DateTime(2020, 12, 2);
var context = new PostContext();
	
await context.Set<Post>()
        .Where(t => t.SubmittedDate.Value.Date == EF.Functions.DateFromParts(date.Year, date.Month, date.Day))
        .CountAsync();

Include verbose output

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (2ms) [Parameters=[@__date_Year_1='2020', @__date_Month_2='2', @__date_Day_3='12'], CommandType='Text', CommandTimeout='30']
      SELECT COUNT(*)
      FROM [Post] AS [c]
      WHERE (CONVERT(date, [c].[SubmittedDate]) = DATEFROMPARTS(@__date_Year_1, @__date_Month_2, @__date_Day_3))

Include provider and version information

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 5.0
Operating system: Windows
IDE: Visual Studio Code 1.51.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions