我有一个大型对象,需要随时加载所有成员。
rtrn.List = Context.Days
.Include(x => x.Inspections.Select(y => y.Type))
.Include(x => x.Inspections.Select(y => y.Subtype))
.Include(x => x.Inspections.Select(y => y.Inspector))
.Include(x => x.Inspections.Select(y => y.Approver))
.Include(x => x.Inspections.Select(y => y.OperatorAddress))
.Include(x => x.Inspections.Select(y => y.Unit))
.Include(x => x.Inspections.Select(y => y.RecordsAddress))
.Include(x => x.Inspections.Select(y => y.InspectionAddress))
.Include(x => x.Inspections.Select(y => y.People))
.Include(x => x.Inspections.Select(y => y.CustomFields))
.Include(x => x.Inspections.Select(y => y.Codelinks.Select(s => s.CodeSections.Select(q => q.Questions.Select(a => a.Answer)))))
.Include(x => x.Inspections.Select(y => y.Attachments))
.Include(x => x.Inspections.Select(y => y.Comments))
.Include(x => x.Inspections.Select(y => y.ComplianceRecords.Select(z => z.Records)))
.Include(x => x.Inspections.Select(y => y.CorrespondenceRecords))
.Where(x => x.Inspector.Id == userId).OrderByDescending(x => x.DayDate).Skip(page * perpage).Take(perpage).ToList();
加载需要一段时间,我正在寻找如何加快速度的想法。
通过我的研究,我看到了使用.Load()
方法而不是包含的建议,所以我尝试了
var rtrn = Context.Days..Where(x => x.Inspector.Id == userId).OrderByDescending(x => x.DayDate).Skip(page * perpage).Take(perpage).ToList();
foreach (Day d in rtrn)
{
Context.Entry(d).Collection(x => x.Inspections).Load();
foreach (Inspection insp in d.Inspections)
{
Context.Entry(insp).Reference(x => x.Type).Load();
Context.Entry(insp).Reference(x => x.Subtype).Load();
Context.Entry(insp).Reference(x => x.Inspector).Load();
Context.Entry(insp).Reference(x => x.Approver).Load();
Context.Entry(insp).Reference(x => x.OperatorAddress).Load();
Context.Entry(insp).Reference(x => x.Unit).Load();
Context.Entry(insp).Reference(x => x.RecordsAddress).Load();
Context.Entry(insp).Reference(x => x.InspectionAddress).Load();
Context.Entry(insp).Collection(x => x.People).Load();
Context.Entry(insp).Collection(x => x.CustomFields).Load();
Context.Entry(insp).Collection(x => x.Codelinks).Load();
Context.Entry(insp).Collection(x => x.Attachments).Load();
Context.Entry(insp).Collection(x => x.Comments).Load();
Context.Entry(insp).Collection(x => x.ComplianceRecords).Load();
Context.Entry(insp).Collection(x => x.CorrespondenceRecords).Load();
foreach (CodeLink cl in insp.Codelinks)
{
Context.Entry(cl).Collection(x => x.CodeSections).Load();
foreach (CodeSection cs in cl.CodeSections)
{
Context.Entry(cs).Collection(x => x.Questions).Load();
foreach (Question q in cs.Questions)
Context.Entry(q).Reference(x => x.Answer).Load();
}
}
foreach (ComplianceRecordBucket b in insp.ComplianceRecords)
Context.Entry(b).Collection(x => x.Records).Load();
}
}
根据我的测量结果,这需要大约10倍的时间。不能说我很惊讶,但这让我不知道在哪里解决这个问题。