Left join via SelectMany + DefaultIfEmpty returns null outer entity instead of just null inner
维护者通常 2 天内回复
@AndriySvyryd 已经在做这个了。
开始于 2026年8月19日。
评估
这个 Issue 还没有评估数据。
描述
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
- 主要语言
- C#
- 星标
- 14.8k
- 派生
- 3.5k
- 平均合并
- 3 天 7 小时
- 30 天内合并 PR
- 182
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/efcore 的其他 Issue
-
customer-reported
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 2 天内回复
-
GroupBy element `OrderBy(...).First()`/`MaxBy` over an owned type mapped to its own table fails to translate可能已有人在做 @cincuranet 今天认领。 未关闭area-groupby area-query
难度 4/5 3-5 天 新手友好度 25/100
dotnet/efcore#39833 · 已指派 1 人 ·
维护者通常 2 天内回复
-
SQLite: Polygon.Length translates to GLength, which returns 0可能已有人在做 @cincuranet 今天认领。 未关闭area-query area-sqlite
难度 3/5 1-2 天 新手友好度 45/100
dotnet/efcore#39826 · 已指派 1 人 ·
维护者通常 2 天内回复
-
Migration lock with LockReleaseBehavior.Transaction doesn't prevent concurrent migrations since per-migration transactions可能已有人在做 @AndriySvyryd 于 1 天前认领。 未关闭area-migrations
dotnet/efcore#39824 · 已指派 1 人 ·
维护者通常 2 天内回复
-
Querying a TPC base type with a JSON complex property throws可能已有人在做 @cincuranet 于 1 天前认领。 未关闭area-query
难度 4/5 3-5 天 新手友好度 22/100
dotnet/efcore#39814 · 已指派 1 人 ·
维护者通常 2 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 77/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
SubtitleEdit/subtitleedit#17247 ·
维护者通常 1 天内回复
-
Restore test cleanup fails on Windows when restored files are read-only可能已有人在做 @JamBalaya56562 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
duplicati/duplicati#8046 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复
-
Bundles Documentation
难度 2/5 1-3 小时 新手友好度 78/100
pyrevitlabs/pyRevit#4285 ·
维护者通常 1 天内回复