I am using Linq to Entities and I would like to only make one call to the database to get some data.
I have an account object which can have multiple companies associated with it. And each company can have many locations. I can get the account with a list of companies no problem. The issue is getting the list of locations for each company and putting it all back into the Account Object. Here is my code so far:
public Account GetAccountByUserId(int userId)
{
IQueryable<Account> oq = _context.Accounts;
oq = oq.Include(x => x.Companies);
oq = oq.Include(x => x.Companies.SelectMany(l => l.Locations); //I was just guessing here, this throws the error below
Account acct = oq.Single(x => x.Users.Any(y => y.Id == userId));
return acct;
}
The error the .SelectMany throws:
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
This might not even be possible with the .Include(), but if not is there another way? I would really like to only make one call to the database.
Have you tried just including the companies and locations like this: