Can you include more than just one relationship level using lambdas in linq to entities

StackOverflow https://stackoverflow.com/questions/23540980

  •  18-07-2023
  •  | 
  •  

Pregunta

I have a data structure in which Users have UserPlans and UserPlans have Plans. I've included a using statement for System.Data.Entity so that I can strongly type my includes, but I can't figure out if there's a way to strongly type an include that goes more than one level deep in the relationships. So I have:

from user in entities.Users.Include(u => u.UserPlans).Include("UserPlans.Plans")

but I'd like to be able to do something like:

from user in entities.Users.Include(u => u.UserPlans).Include(u => u.UserPlans.Include(up => up.Plan))

In order not to have the string in my include. Is there a way to do so?

¿Fue útil?

Solución

Looks like this has been answered before: EF CTP5 - Strongly-Typed Eager Loading - How to Include Nested Navigational Properties?

Basically:

from user in entities.Users.Include(u => u.UserPlans.Select(p => p.Plans))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top