7

I am trying to eager load properties of a derived class in an Entity Framework model.

I read all over the place that I have to first filter the set with OfType() before including properties with Include():

var persons = Context.Persons
                     .OfType<Employee>()
                     .Include("Compensation")

I don't know how to get that Include() to work though because in my case, Persons is a DbSet, OfType() returns an IQueryable and IQueryable does not define an Include() method.

Community
  • 1
  • 1
Xavier Poinas
  • 19,377
  • 14
  • 63
  • 95
  • Hope this link help http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/2130796d-a2fe-4137-af18-d56480748384 – Boomer Aug 23 '12 at 05:50

1 Answers1

16

Place this:

using System.Data.Entity;

into your using's list, and after that you will be able to use Include extension methods family from DbExtensions class:

    public static IQueryable<T> Include<T, TProperty>(this IQueryable<T> source, Expression<Func<T, TProperty>> path) where T : class;
    public static IQueryable<T> Include<T>(this IQueryable<T> source, string path) where T : class;
    public static IQueryable Include(this IQueryable source, string path);

They accept IQueryable as the first argument, and there are strongly-typed ones, too, which is better, then Include(String).

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • 2
    Ah! Thanks, I thought it was probably an extension method. I really wish that VS made the namespace discoverable and/or that people included the usings in their samples :) – Xavier Poinas Aug 23 '12 at 06:08
  • @XavierPoinas: completely agree with you, this confused me too first time. – Dennis Aug 23 '12 at 06:15
  • @XavierPoinas I've been converted to Resharper for this (amongst other) reasons. Resharper will suggest extension methods to fill this missing gap in Visual Studios suggestions in intellisense. Totally agree with you! – The Senator Aug 25 '15 at 18:24
  • 1
    @TheSenator Actually VS 2015 now does that too so I'm pretty happy now :-) – Xavier Poinas Aug 27 '15 at 06:20
  • @XavierPoinas: true. Ultimately, VS will kill R#. :) – Dennis Aug 27 '15 at 06:22