Skip to content

Query: consider expanding null safety beyond nav rewrite #12284

Closed as not planned
Closed as not planned

Description

@AlekseyMartynov

Version: 2.1.1-rtm-30843 (nightly build)

Please consider this example:

Entity:

public class TestEntity {
    public int ID { get; set; }
    public DateTime? Date { get; set; }
}

Ensure there are records:

var set = context.Set<TestEntity>();
set.Add(new TestEntity());
context.SaveChanges();

Now, this is a query that is fully translated into SQL:

var result = set
    .GroupBy(obj => new {
        Year = (int?)obj.Date.Value.Year
    })
    .Select(g => new {
        g.Key.Year,
        Count = g.Count()
    })
    .ToArray();
SELECT DATEPART(year, [obj].[Date]) AS [Year], COUNT(*) AS [Count]
FROM [TestEntity] AS [obj]
GROUP BY DATEPART(year, [obj].[Date])

However, if I modify it as follows:

var result = set
    .GroupBy(obj => new {
        Year = (int?)obj.Date.Value.Year
    })
    .Select(g => new {
        g.Key.Year,
        Count = g.Count(i => i.Date != null) // change here: count only non-null dates
    })
    .ToArray();

then it is partially translated into SQL:

SELECT [obj].[ID], [obj].[Date]
FROM [TestEntity] AS [obj]
ORDER BY DATEPART(year, [obj].[Date])

but grouping is done in-memory.

It fails with Nullable object must have a value.

I understand that for LINQ 2 Objects I would have to add null-conditional:

Year = obj.Date == null ? null : (int?)obj.Date.Value.Year

Finally the question:

Should the user of EF Core proactively take care of null safety as with L2O? Or on the contrary it is a bug and EF Core in-memory magic must hide it from me?

Activity

  1. ajcvickers commented on Jun 8, 2018

    @ajcvickers
    Contributor

    We do perform some re-writing already to cover some similar cases. We will consider enhancing this for 3.0.

  2. added this to the 3.0.0 milestone on Jun 8, 2018
  3. changed the title [-]Question regarding the necessity of null checks depending on in-memory or SQL evaluation[/-] [+]Query: consider expanding null safety beyond nav rewrite[/+] on Aug 3, 2018
  4. smitpatel commented on Jan 25, 2019

    @smitpatel
    Contributor

    In the absence of client eval, this may not provide much value.

  5. AlekseyMartynov commented on Jan 25, 2019

    @AlekseyMartynov
    Author

    @smitpatel

    In the absence of client eval, this may not provide much value.

    Yes, this ticket is about cases where a query is executed partially in SQL and partially in memory.
    Are you planning to drop client evaluation in 3.0?

  6. smitpatel commented on Jan 25, 2019

    @smitpatel
    Contributor

    Are you planning to drop client evaluation in 3.0?

    Brief answer, we are dropping it for most cases.
    I will post long answer on #12795 soon.

  7. removed this from the 3.0.0 milestone on Feb 6, 2019
  8. removed their assignment
    on Jan 12, 2022
  9. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions