Question

I have a model that looks something like this:

public abstract class Parent
{
    public int Id { get; set; }
}

public class Child11 : Parent
{}

public class Child2 : Parent
{
    public virtual Dependency Dependency { get; set; }
}

public class Dependency
{
    public int Id { get; set; }
    public virtual ICollection<Child2> Children { get; set; }
}

I'm trying to figure out who to write a Linq query that loads all Parents and eager loads Dependency on all Child2's. Is this even possible? I've tried every combination of Linq statements I can think of and have had no success.

Was it helpful?

Solution

If you need to load just child2 you can use:

var child2 = context.Parents
                    .OfType<Child2>()
                    .Include(c => c.Dependency)
                    .ToList();

If you also need to load all other derived types you will most probably need to use second query or try to make some union. Polymorphic queries in EF don't work very well with eager loading.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top