Skip to content

NavigationExpandingExpressionVisitor moves Select() behind DefaultIfEmpty()Β #36208

Closed

Description

@Ta1sty

Question

Hello,

I am currently writing an EntityFramework Provider. While translating the expression produced by NorthwindSelectQueryTestBase.SelectMany_correlated_with_outer_3, I noticed that the NavigationExpandingExpressionVisitor moves the .Select(o => c.City) behind the .DefaultIfEmpty().

I'm probably missing something, but this seems wrong, as
Enumerable.Empty().DefaultIfEmpty().Select(x => "foo")
is not the same as
Enumerable.Empty().Select(x => "foo").DefaultIfEmpty()

So now I'm left wondering how to handle this, because there are valid uses for both cases. Once the NavigationExpandingExpressionVisitor transforms the expression however I can no longer distinguish between these cases.

I would be very thankful if you could give me some pointers on what the actual behaviour should be.

Best Regards

Your code

NorthwindSelectQueryTestBase.SelectMany_correlated_with_outer_3:

from c in ss.Set<Customer>()
from o in ss.Set<Order>().Where(o => c.CustomerID == o.CustomerID).Select(o => c.City).DefaultIfEmpty()
select new { c, o }

Here's what the query looks like just before and after the call to the NavigationExpandingExpressionVisitor in QueryTranslationPreprocessor.cs Process(Expression query)

Before NavigationExpandingExpressionVisitor:

[Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression]
  .SelectMany(c => 
    [Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression]
      .Where(o => (c.CustomerID == o.CustomerID))
      .Select(o => c.City)
      .DefaultIfEmpty(), 
    (c, o) => new <>f__AnonymousType0`2(c = c, o = o)
  )

After NavigationExpandingExpressionVisitor:

[Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression]
  .SelectMany(c => 
    [Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression]
      .Where(o => (c.CustomerID == o.CustomerID))
      .DefaultIfEmpty()
      .Select(o => c.City),
    (c, c) => new TransparentIdentifier`2(Outer = c, Inner = c))
  .Select(ti => new <>f__AnonymousType0`2(c = ti.Outer, o = ti.Inner))

Stack traces


Verbose output


EF Core version

9.0.0

Database provider

No response

Target framework

.NET 9.0

Operating system

No response

IDE

No response

Activity

  1. self-assigned this
    on Jun 9, 2025
  2. added theissue type on Jun 9, 2025
  3. roji commented on Jun 9, 2025

    @roji
    Member

    Thanks, this indeed looks like a bug in EF itself (in NavigationExpandingExpressionVisitor).

    Minimal repro
    await using var context = new BlogContext();
    await context.Database.EnsureDeletedAsync();
    await context.Database.EnsureCreatedAsync();
    
    // Produces [null]
    var expected = Enumerable.Empty<Blog>().Select(c => "foo").DefaultIfEmpty().ToList();
    
    // Incorrect result: produces ["foo"] instead of [null]
    var actual = await context.Blogs.Select(c => "foo").DefaultIfEmpty().ToListAsync();
    
    public class BlogContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
  4. added this to the 10.0.0 milestone on Jun 12, 2025
  5. added 5 commits that reference this issue on Jun 12, 2025
    9a89aa8
    02a6730
    67e5859
    bb3024a
    c44c567
  6. added a commit that references this issue on Jun 23, 2025
    35cd115
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions