Spiegherò il mio problema usando un esempio. Diciamo che ho le seguenti classi e metodi (li ho creati solo per questo esempio)
public class Student
{
public string Name { get; set; }
public string Id { get; set; }
public Subject Expertise { get; set; }
}
public class Subject
{
public string Name { get; set; }
public Teacher Teacher { get; set; }
}
public class Teacher
{
public string Name { get; set; }
public string LicenseId{ get; set; }
public License License { get; set; }
}
public class License
{
public string LicsenseType;
}
public static IQueryable<Subject> PopulateWithTeacherAndLicense(this IQueryable<Subject> subjects)
{
return subjects
.Include(c => c.Teacher)
.ThenInclude(p => p.License);
}
Ora supponiamo di voler selezionare tutti gli studenti con tutta la loro materia, insegnante e licenza. Per fare ciò, voglio usare il mio PopulateWithTeacherAndLicense. Voglio che la query assomigli a qualcosa:
db.Students.Include(s => s.Expertise.PopulateWithTeacherAndLicense())
E non devi fare Include (s => s.Expertise). TheInclude (s => s.Teacher) ...
È possibile creare il metodo di estensione per la raccolta di studenti stessa
public static IQueryable<Student> IncludeExpertise(this IQueryable<Student> students)
{
return students
.Include(s => s.Expertise)
.ThenInclude(c => c.Teacher)
.ThenInclude(p => p.License);
}