如何拆分多个linq include()方法进行优化查询:
allRecords = context.Records.Where(r => r.versionId == version.id)
.Include(x => x.colors)
.Include(x => x.tags)
.Include(x => x.ranks)
.OrderBy(r => r.sortkey).ToList();
如何通过将查询细分为更简单的查询来优化此查询? 该请求太慢。有必要提高其性能。
答案 0 :(得分:1)
您可以使用例如一些布尔标志可以做到这一点:
var query = context.Records.Where(r => r.versionId == version.id);
if (includeColors) query = query.Include(x => x.colors);
if (includeTags ) query = query.Include(x => x.tags);
if (includeRanks ) query = query.Include(x => x.ranks);
allRecords = query.OrderBy(r => r.sortkey).ToList();