在EF核心中,我试图用子集合来计划结果集.
ctx.As.Select(a => new { AId = a.Id,BNames = a.Bs.Select(x=>x.Name) //SubCollection })
但是,当执行此操作时,BNames集合是惰性的,因此当它被枚举时,它会触发每行的单独查询.例如,对于B集合中的2个项目.
[a].[Id] AS [AId] FROM [As] AS [a] p_executesql N'SELECT [x].[Name] FROM [Bs] AS [x] WHERE @_outer_Id = [x].[AId]',N'@_outer_Id int',@_outer_Id=1 p_executesql N'SELECT [x].[Name] FROM [Bs] AS [x] WHERE @_outer_Id = [x].[AId]',@_outer_Id=2
在EF6中,相同的查询会产生以下结果(正如我所料):
SELECT [Extent1].[Id] AS [Id],[Project1].[C1] AS [C1],[Project1].[Name] AS [Name] FROM [dbo].[A] AS [Extent1] LEFT OUTER JOIN (SELECT [Extent2].[Name] AS [Name],[Extent2].[A_Id] AS [A_Id],1 AS [C1] FROM [dbo].[B] AS [Extent2] ) AS [Project1] ON [Extent1].[Id] = [Project1].[A_Id] ORDER BY [Extent1].[Id] ASC,[Project1].[C1] ASC
如何让EF热切地加载子集合BName?
注意:这与包含.Include的子实体不同,因为这是自定义投影,而不是导航属性.在此处使用.Include会导致错误.
完整的回购代码:https://gist.github.com/lukemcgregor/692834629da09b21d5a35515e86c9002
解决方法
这在EF Core 2.1中可以使用其相关的子查询优化.
您需要更新到EF Core 2.1并进行小修改以通过向子查询添加.ToList()来选择优化:
foreach (var thing in ctx.As .Select(a => new { AId = a.Id,BNames = a.Bs.Select(x => x.Name).ToList() }))
见New features in EF Core 2.1中的Optimization of correlated subqueries.