Take the 2-minute tour ×

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.

share|improve this question
and what's wrong ? The query is crashing, you've got an error, you ain't got no result ? –  Raphaël Althaus Apr 13 '12 at 15:28
Sorry about that, I have edited my original post –  user962926 Apr 13 '12 at 15:57
add comment

1 Answer

up vote 1 down vote accepted

Have you tried just including the companies and locations like this:

share|improve this answer
Perfect! Thank you! –  user962926 Apr 16 '12 at 17:39
add comment

Your Answer


