0

Say my object graph looks like:

User
 => Friends
   => Clicks
     => Urls

So when I load a User, I also want to eagirly load the navigation property Friends. And I want the friend object to eagirly load Clicks and so on.

Using the code I have now I can only do this for 1 level:

public User GetUserById(int userId)
{
  return Get(x => x.Id == userId, includeProperties: "Friends").FirstOrDeafult();
}

Is this possible?

Slauma
  • 175,098
  • 59
  • 401
  • 420
loyalflow
  • 14,275
  • 27
  • 107
  • 168

1 Answers1

2

Apparently this repository implementation is just splitting the includeProperties parameter by comma (includeProperties.Split(new char[] { ',' }) and then calls

query = query.Include(includeProperty);

for each element in the result array of the split. For your example you can use a dotted path then:

return Get(x => x.Id == userId, includeProperties: "Friends.Clicks.Urls")
    .FirstOrDefault();

It will load all entities on the path from the root User entity to the last navigation property Urls.

If you had another navigation property in User - say Profile - and you would want to eagerly load that as well, it seems that this syntax is supported then:

return Get(x => x.Id == userId, includeProperties: "Profile,Friends.Clicks.Urls")
    .FirstOrDefault();
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • What about setting the orderby for the child collections when they load? is this possible? – loyalflow Jul 15 '13 at 16:15
  • @user1361315: It's not directly possible, there are only some workarounds: http://stackoverflow.com/questions/7522784/ef-4-1-code-first-how-to-order-navigation-properties-when-using-include-and-or – Slauma Jul 15 '13 at 16:34