I would like to store a set of .include() statements in a variable since I need to use the same includes in multiple locations. So in an attempt to prevent code duplication, here is what I am asking:
For example, take this following query where I eager load several related entities:
var record = await db.DisplacementDamageRecords
.Include(tr => tr.Mission)
.ThenInclude(m => m.MissionRadiationPartsLead)
.Include(tr => tr.Mission)
.ThenInclude(m => m.MissionInstruments)
.ThenInclude(mi => mi.Instrument)
.Include(tr => tr.Mission)
.ThenInclude(m => m.MissionInstruments)
.ThenInclude(mi => mi.CognitiveEngineer)
.Include(tr => tr.ProcurementPart)
.ThenInclude(pp => pp.Part)
.FirstOrDefaultAsync(tr => tr.Id == form.Id);
It would be useful to extract the includes like so:
var testRecordIncludes =
.Include(tr => tr.Mission)
.ThenInclude(m => m.MissionRadiationPartsLead)
.Include(tr => tr.Mission)
.ThenInclude(m => m.MissionInstruments)
.ThenInclude(mi => mi.Instrument)
.Include(tr => tr.Mission)
.ThenInclude(m => m.MissionInstruments)
.ThenInclude(mi => mi.CognitiveEngineer)
.Include(tr => tr.ProcurementPart)
.ThenInclude(pp => pp.Part)
So that I could then use this variable like so:
var record = await db.DisplacementDamageRecords
.testRecordIncludes()
.FirstOrDefaultAsync(tr => tr.Id == form.Id);
Is this possible? Thanks!