Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cast datetimeoffet to date then check if in list of dates in linq/c# #15208

Closed
ghost opened this issue Mar 30, 2019 · 4 comments · Fixed by #16344
Closed

cast datetimeoffet to date then check if in list of dates in linq/c# #15208

ghost opened this issue Mar 30, 2019 · 4 comments · Fixed by #16344
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@ghost
Copy link

ghost commented Mar 30, 2019

This is not an issue, more of an documentation i'm not sure where to find.

i have a table with datetimeoffset column. i want to query
where

@start <= MyDateTimeOffsetCol AND MyDateTimeOffsetCol < @end
CAST(MyDateTimeOffsetCol as date) IN (@d0, @d1, ...)

Questions:

  1. How is this done in EF Core?
  2. should I just use raw sql?
  3. should I concatenate OR clause? how is this done in EF core for scoped OR inside AND @start <= MyDateTimeOffsetCol AND MyDateTimeOffsetCol < @end AND (MyDateTimeOffsetCol=@d0 OR MyDateTimeOffsetCol=@d1 OR ...)
  4. just out of curiosity is this good practice if the count of the content of the list of dates d0, d1,... is about 30 to 300? assuming Time is indexed

I tried

Steps to reproduce

.Where(o => o.Time.Date == cSharpDateTime)

works

but

List<DateTime> dates = ...
.Where(o => 
@start <= o.Time && o.Time < @end &&
dates.Contains(o.Time.Date))

didn't get converted to sql

List<DateTime> dates = ...
.Where(o => 
@start <= o.Time && o.Time < @end
dates.Contains(o.Time.Date))

Further technical details

EF Core version: "Microsoft.EntityFrameworkCore.SqlServer": "2.2.0"
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: VS Code 1.32.3

@divega divega added this to the 3.0.0 milestone Apr 1, 2019
@divega divega added the type-bug label Apr 1, 2019
@smitpatel smitpatel added the verify-fixed This issue is likely fixed in new query pipeline. label May 26, 2019
@roji
Copy link
Member

roji commented Jun 28, 2019

Confirmed that SQL gets properly generated as of a2ce460 (near preview7).

Not committing QueryBugsTest repro as it doesn't seem necessary, but here is the code used:

#region Bug15208

[ConditionalFact]
public virtual void Bug_name()
{
    using (CreateDatabase15208())
    {
        using (var context = new MyContext15208(_options))
        {
            var (start, end) = (default(DateTimeOffset), default(DateTimeOffset));
            var dates = new List<DateTimeOffset> { DateTimeOffset.Now };
            var test = context.Blogs
                .Where(b =>
                    start <= b.PublicationDate.Date &&
                    b.PublicationDate < end &&
                    dates.Contains(b.PublicationDate))
                .ToList();
        }
    }
}

private SqlServerTestStore CreateDatabase15208()
    => CreateTestStore(
        () => new MyContext15208(_options),
        context =>
        {

            ClearLog();
        });

public class MyContext15208 : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    public MyContext15208(DbContextOptions options) : base(options) {}

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

public class Blog
{
    public int Id { get; set; }
    public DateTimeOffset PublicationDate { get; set; }
}

#endregion Bug15208

SQL:

SELECT [b].[Id], [b].[PublicationDate]
FROM [Blogs] AS [b]
WHERE ((@__start_0 <= CAST(CONVERT(date, [b].[PublicationDate]) AS datetimeoffset)) AND ([b].[PublicationDate] < @__end_1)) AND [b].[PublicationDate] IN ('2019-06-28T18:56:51.5342510+02:00')

@roji roji closed this as completed Jun 28, 2019
@roji roji added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed propose-punt verify-fixed This issue is likely fixed in new query pipeline. labels Jun 28, 2019
@smitpatel
Copy link
Contributor

Do we have regression test?

@smitpatel smitpatel reopened this Jun 28, 2019
@roji
Copy link
Member

roji commented Jun 28, 2019

As I wrote above it didn't seem necessary for this simple case (test posted above). But if we want a test for every bug, I'll submit a PR, no problem.

@smitpatel
Copy link
Contributor

We should only close without adding test if we have existing test which covers it.

roji added a commit that referenced this issue Jun 28, 2019
DateTimeOffset and Contains()

Fixes #15208
roji added a commit that referenced this issue Jun 28, 2019
DateTimeOffset and Contains()

Fixes #15208
roji added a commit that referenced this issue Jun 28, 2019
DateTimeOffset and Contains()

Fixes #15208
roji added a commit that referenced this issue Jun 28, 2019
DateTimeOffset and Contains()

Fixes #15208
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview7 Jul 2, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview7, 3.0.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants