Eager loading property of derived class using Include

前端 未结 4 1543
我在风中等你
我在风中等你 2020-12-09 10:41

I have classes like:

Person
{
   Name
   Address
}

Employee : Person
{
   Compensation - object
}

Visitor : Person
{

}

If I write linq:<

相关标签:
4条回答
  • 2020-12-09 11:06

    This horrible thing works:

    var persons = context.Persons
                    .Concat(context.Employees
                        .Where(e => e.Compensation.Amount >= 0))
                    .Concat(context.Visitors
                        .Where(v => v.SomeProperty == "AlwaysTrue"));
    

    I'm not sure why, but when you filter on an object property, the property's object is eagerly loaded. If you don't want to filter that property, then use a condition that will always be true.

    Disclaimer: I have no idea how efficient the resulting query is. I checked the sql generated when I tested this in a slightly more complicated scenario and it was very very large.

    0 讨论(0)
  • 2020-12-09 11:07

    You can try it this way:

    var persons = Context.Persons
                         .OfType<Employee>()
                         .Include("Compensation")
                         .Concat<Person>(Context.Persons.OfType<Visitor>());
    
    0 讨论(0)
  • 2020-12-09 11:20

    Here's a nice example of how to load Persons and include Compensation for Employees

    Replace Reference() by Collection() for a collection property.

    IQueryable<Person> GetPersons()
    {
        var persons = Context.Persons;
        foreach(var entry in persons.OfType<Employee>())
            Context.Entry(entry).Reference(e => e.Compensation).Load();
        return persons;
    }
    

    Not sure either if it's efficient, but it works, and the intention is clearer than with joins.

    Based on this SO answer

    0 讨论(0)
  • 2020-12-09 11:27
    var employees = context.Persons.OfType<Employee>().Include(x => x.Compensation).ToArray();
    
    var nonEmployees = context.Persons.Except(context.Persons.OfType<Employee>()).ToArray();
    
    var people = employees.Concat(nonEmployees);
    
    0 讨论(0)
提交回复
热议问题