Skip to content

Translation issue with LocalDate in Where clause #1969

Closed

Description

@mhdubose

I have a LocalDate column in the database capturing an expected date for an event to occur. I'm attempting to structure a query that retrieves rows that are late records by comparing to today's date. I'm running into translation issues when adding this to the where clause however.

I can get a projection to work properly. Both of the following return the proper value:

var sample = context.Samples.Select(s => new {
    DateDiff = DateTime.Today - s.EventDate.ToDateTimeUnspecified()
});

and

var sample = context.Samples.Select(s => new {
    DateDiff = LocalDate.FromDateTime(DateTime.Today) - s.EventDate
});

One of these returns a Period, the other a TimeSpan, obviously, but I can get what I need from both. The problem arises when adding a where clause. When I want to filter on rows that are greater than a particular diff, the translation starts failing

var sample = sample.Where(s => s.DateDiff.TotalDays > 2);

results in

Translation of method 'NodaTime.LocalDate.ToDateTimeUnspecified' failed

and

var sample = sample.Where(s => s.DateDiff.Days > 2);

results in

Translation of method 'NodaTime.LocalDate.FromDateTime' failed

The same issue occurs if I return the TotalDays/Days rather than the TimeSpan/Period. The issue also occurs if I remove the projection and simply attempt to add the full calculation and conditional in a Where clause

Because I'm paginating the results, comparing the Period/Timespan client side is not a viable option.

Activity

  1. roji commented on Sep 6, 2021

    @roji
    Member

    Sorry for not answering this earlier - I'm currently doing a lot of work on timestamp and NodaTime handling for 6.0 - I'll give this a deep look in the coming days.

  2. roji commented on Sep 7, 2021

    @roji
    Member

    @mhdubose thanks for your patience - I finally looked at this.

    First, ideally you want to avoid using the standard DateTime if you've decided to use NodaTime (which is a great decision) - it's best to get the today's date via the NodaTime APIs, and not via DateTime.Today.

    Second, you say you want to fetch rows based on today's date, but how do you want to calculate today's date, i.e. based on which time zone? Your options are (a) based on the client's system timezone (where the .NET application is running, (b) based on the PostgreSQL configured time zone (TimeZone connection parameter), (c) UTC or (d) some other arbitrary time zone. This can be quite important, as dates will changed based on the timezone you choose.

    If you're looking for (a), then getting a LocalDate representing today in NodaTime is done as follows:

    var localTimeZone = DateTimeZoneProviders.Tzdb.GetSystemDefault();
    var today = SystemClock.Instance.GetCurrentInstant().InZone(localTimeZone).Date;

    This isn't something you can put in an EF LINQ query (yet!), but you can send that as a parameter:

    _ = ctx.Blogs.Where(b => (b.Date - today).Days > 1).ToList();

    Unfortunately there's a small bug in date subtraction - I've opened #1979 and a fix will go into the next patch version of the EF Core provider (should be very soon).

    Hopefully that makes things clear... Post back to let me know if this answers your question, or if you need to e.g. get today's date under UTC instead.

  3. roji commented on Sep 7, 2021

    @roji
    Member

    Am closing this as a dup of #1979 for now, but don't hesitate to post back here with any questions etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions