My schema includes:
I tried using the following in my repository for LoanApplication:
public ILoanApplication GetById(int id)
{
ILoanApplication item;
using (var db = new MainContext())
{
item = db.LoanApplications
.Include(c => c.Borrowers)
.Include(c => c.Borrowers.Select(b => b.BusinessBorrowerIndividual))
.Include(c => c.Borrowers.Select(b => b.BorrowersOwnerManagers))
.Include(c => c.Borrowers.Select(b => b.BorrowersOwnerManagers.Select(bb => bb.OwnerManager)))
.Include(c => c.Borrowers.Select(b => b.BorrowersOwnerManagers.Select(bb => bb.OwnerManager)
.OfType<OwnerManagerIndividual>()
.Select(o => o.OwnerManagerIndividualsRaceTypes)))
.FirstOrDefault(l => l.Id == id);
}
return item;
}
Everything works except for the last include. I receive the following error when I use it:
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path
How do I update so that I can include the OwnerManagerIndividualsRaceTypes properties for the OwnerManager objects that are of OwnerManagerIndividual type? I also want to include all OwnerManger objects, regardless of if they are OwnerManagerIndividual or OwnerManagerBusiness.
** Update ** Based on the responses, it appears it can't easily be done in a single call. I going with the suggestion to make a secondary call to populate, which isn't as elegant solution:
public ILoanApplication GetById(int id)
{
var ownerManagerIndividualRepository = new OwnerManagerIndividualRepository(UserOrProcessName);
ILoanApplication item;
using (var db = new MainContext())
{
item = db.LoanApplications
.Include(c => c.Borrowers)
.Include(c => c.Borrowers.Select(b=> b.BusinessBorrowerIndividual))
.Include(c => c.Borrowers.Select(b => b.BorrowersOwnerManagers))
.Include(c => c.Borrowers.Select(b => b.BorrowersOwnerManagers.Select(bb => bb.OwnerManager)))
.FirstOrDefault(l => l.Id == id);
}
if (item?.Borrowers?.Count > 0 &&
item.Borrowers.FirstOrDefault().BorrowersOwnerManagers.Any(o => o.OwnerManager.IsIndividual))
{
foreach (var owners in item.Borrowers.FirstOrDefault().BorrowersOwnerManagers.Where(o => o.OwnerManager.IsIndividual))
{
var owner = ownerManagerIndividualRepository.GetById(owners.OwnerManager.Id.Value);
foreach (var type in owner.OwnerManagerIndividualsRaceTypes)
{
((OwnerManagerIndividual)owners.OwnerManager).OwnerManagerIndividualsRaceTypes.Add((OwnerManagerIndividualsRaceTypes)type);
}
}
}
return item;
}
.Include()
extension serves just single purpose: load properties eagerly instead of leaving them for lazy loading. That's just an option for query builder to include JOIN
s in SQL, not anything else.
You are trying to provide filtering inside Include
expression, which is wrong.
As for how to actually achieve filtering part of inner collection I'm not sure that's easily doable with single query in EF, you can check similar question for example How to filter nested collection Entity Framework objects?