Left join via SelectMany + DefaultIfEmpty returns null outer entity instead of just null inner
Los mantenedores suelen responder en 2 días
@AndriySvyryd ya está trabajando en esto.
Desde el 19/8/2026.
Evaluación
Este issue todavía no se ha evaluado.
Descripción
Bug description
Projecting an anonymous type that captures both the outer entity and the inner element inside SelectMany(s => s.Nav.Take(1).DefaultIfEmpty().Select(f => new { s, f })) returns rows in which the outer entity s is null, even though s comes from the outer table and its navigation is a required, non-nullable reference. The nulls occur on exactly the rows whose inner collection is empty, and the equivalent Select(s => new { s, s.Nav.Take(1).FirstOrDefault() }) query over the same data materializes all 40 outer entities correctly
("equivalent" in terms of results, not in terms of the same translated query)
This occurs in both EFC 10.0.11, and the EFC 11.0.0-preview.7.26381.103
Related to what was discussed in #30450
Your code
Full MRE:
https://github.com/AlmightyMRE/EFC_DefaultIfEmpty_Bug/blob/main/Program.cs
var ids = await db.OCR.Where(x => x.Status == ProcessStatus.Pending).Select(x => x.Id).Take(40).ToListAsync();
var query = db.OCR.Where(x => ids.Contains(x.Id));
var broken = await query.SelectMany(s => s.FileData.UsedByFiles.Take(1).DefaultIfEmpty().Select(f => new { FileOcr = s, File = f })).ToListAsync();
var control = await query.Select(s => new { FileOcr = s, File = s.FileData.UsedByFiles.Take(1).FirstOrDefault() }).ToListAsync();
Console.WriteLine($"SelectMany+DefaultIfEmpty: rows {broken.Count}, {broken.Count(q => q.FileOcr is null)} FileOcr nulls, {broken.Count(q => q.File is null)} File nulls");
Console.WriteLine($"Select+FirstOrDefault: rows {control.Count}, {control.Count(q => q.FileOcr is null)} FileOcr nulls, {control.Count(q => q.File is null)} File nulls");
Output:
SelectMany+DefaultIfEmpty: rows 40, 14 FileOcr nulls, 14 File nulls
Select+FirstOrDefault: rows 40, 0 FileOcr nulls, 14 File nulls
EF Core version
10.0.11
Database provider
Microsoft.EntityFrameworkCore.SqlServer
Target framework
.NET 10
Operating system
Windows 11
IDE
JetBrains Rider
- Lenguaje dominante
- C#
- Estrellas
- 14.8k
- Forks
- 3.5k
- Merge medio
- 3 d 7 h
- PR fusionados (30 d)
- 182
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de dotnet/efcore
-
customer-reported
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 2 días
-
GroupBy element `OrderBy(...).First()`/`MaxBy` over an owned type mapped to its own table fails to translatePosiblemente ocupada @cincuranet la tomó hoy. Abiertoarea-groupby area-query
Dificultad 4/5 3-5 días Aptitud para principiantes 25/100
dotnet/efcore#39833 · 1 asignado ·
Los mantenedores suelen responder en 2 días
-
SQLite: Polygon.Length translates to GLength, which returns 0Posiblemente ocupada @cincuranet la tomó hoy. Abiertoarea-query area-sqlite
Dificultad 3/5 1-2 días Aptitud para principiantes 45/100
dotnet/efcore#39826 · 1 asignado ·
Los mantenedores suelen responder en 2 días
-
Migration lock with LockReleaseBehavior.Transaction doesn't prevent concurrent migrations since per-migration transactionsPosiblemente ocupada @AndriySvyryd la tomó hace 1 día. Abiertoarea-migrations
dotnet/efcore#39824 · 1 asignado ·
Los mantenedores suelen responder en 2 días
-
Querying a TPC base type with a JSON complex property throwsPosiblemente ocupada @cincuranet la tomó hace 1 día. Abiertoarea-query
Dificultad 4/5 3-5 días Aptitud para principiantes 22/100
dotnet/efcore#39814 · 1 asignado ·
Los mantenedores suelen responder en 2 días
Todos los issues de dotnet/efcore
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
SubtitleEdit/subtitleedit#17247 ·
Los mantenedores suelen responder en 1 día
-
Restore test cleanup fails on Windows when restored files are read-onlyPosiblemente ocupada @JamBalaya56562 la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
duplicati/duplicati#8046 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 1 día
-
Bundles Documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
pyrevitlabs/pyRevit#4285 ·
Los mantenedores suelen responder en 1 día