且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EF Core嵌套的Linq选择结果在N + 1个SQL查询中

更新时间:2023-01-21 08:28:44

GitHub问题#4007 已标记为里程碑2.1.0-preview1closed-fixed.现在,可以在 NuGet href =".NET博客文章.

The GitHub issue #4007 has been marked as closed-fixed for milestone 2.1.0-preview1. And now the 2.1 preview1 has been made available on NuGet as discussed in this .NET Blog post.

版本2.1正确的版本也已发布,请使用以下命令进行安装:

Version 2.1 proper is also released, install it with the following command:

Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.1.0

然后在嵌套的.Select(x => ...)上使用.ToList()指示应立即获取结果.对于我的原始问题,它看起来像这样:

Then use .ToList() on the nested .Select(x => ...) to indicate the result should be fetched immediately. For my original question this looks like this:

var query = context.Top
    .Select(t => new {
        prop1 = t.C1,
        prop2 = t.Sub.Select(s => new {
            prop21 = s.C3
        })
        .ToList() // <-- Add this
    });
var res = query.ToArray(); // Execute the Linq query

这将导致在数据库上运行2个SQL查询(而不是N + 1);首先是基于Key-ForeignKey关系SELECT FROM'Top'表,然后是SELECT FROM'Sub'表和INNER JOIN FROM'Top'表>.然后将这些查询的结果合并到内存中.

This results in 2 SQL queries being run on the database (instead of N + 1); First a plain SELECT FROM the 'Top' table and then a SELECT FROM the 'Sub' table with an INNER JOIN FROM the 'Top' table, based on Key-ForeignKey relation [Sub].[TopId] = [Top].[Id]. The results of these queries are then combined in memory.

结果正好符合您的期望,并且与EF6所返回的结果非常相似:匿名类型'a的数组,其属性为prop1prop2,其中prop2是匿名类型的列表'b具有属性prop21.最重要的是所有这些都在.ToArray()调用后全部加载!

The result is exactly what you would expect and very similar to what EF6 would have returned: An array of anonymous type 'a which has properties prop1 and prop2 where prop2 is a List of anonymous type 'b which has a property prop21. Most importantly all of this is fully loaded after the .ToArray() call!