c# - 带有 n+1 问题的 LINQ 查询
问题描述
我有一个包含三个子查询的查询,我的问题是子查询是针对每个国家 (n+1) 运行的。
我已经简化了查询以使其更易于阅读,因为主查询大约有 70 行,并更改了域以使其更易于理解。
我试过包括 Cities/Mountains/Rivers 并在子查询上运行 .ToList() ,但无济于事。
// The CountryDto class I'm selecting to.
public class CountryDto
{
public string CountryName { get; set; }
public IEnumerable<CityDto> CityDtos { get; set; }
public IEnumerable<MountainDto> MountainDtos { get; set; }
public IEnumerable<RiverDto> RiverDtos { get; set; }
}
// The query
var query = _db.Countries
.Select(country => new CountryDto
{
CountryName = country.Name,
CityDtos = country.Citites
.Where(city => city.Population > 10000)
.Select(city => new CityDto
{
Name = city.Name,
}),
MountainDtos = country.Mountains
.Where(mountain => mountain.Height > 100)
.Select(mountain => new MountainDto
{
Name = mountain.Name,
}),
RiverDtos = country.Rivers
.Where(river => river.Length > 1000)
.Select(river => new RiverDto
{
Name = river.Name,
}),
})
.Where(c => c.CityDtos.Any() || c.MountainDtos.Any() || c.RiverDtos.Any());
var totalCount = query.Count();
var countries = await query.ToListAsync();
解决方案
Entity Framework Core supports parts of the query being evaluated on the client and parts of it being pushed to the database. It is up to the database provider to determine which parts of the query will be evaluated in the database.
In your case, all .Any
parts are evaluated in the client side I guess. You can configure your code to throw an exception for client evaluation.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFQuerying;Trusted_Connection=True;")
.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
}
For more info https://docs.microsoft.com/en-us/ef/core/querying/client-eval
推荐阅读
- react-native-navigation - 使用 RNN 库重置堆栈
- android - 从 Google Play 商店安装应用程序后如何打开特定活动以获取深层链接
- javascript - 如何将 javascript 文件插入到我的 IE-11 的 BHO 扩展中?我想插入一个多行的 javascript 文件
- bash - 如何在同一行附加标准错误
- c# - SeleniumExtras.WaitHelpers.ExpectedConditions
- docker - Tomcat docker 容器日志在几个小时后挂起
- docker - Jenkins docker 容器总是添加 cat 命令
- java - 为什么我们在 Java 中初始化数组对象时不必提供括号?
- dart - 如何添加 Flutter DropdownButtonFormField
- flutter - 当插件在java中创建新线程时颤动粉碎