How to use Include () after OfType ()?

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

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

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

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

+7
source share
1 answer

Put this:

 using System.Data.Entity; 

into your usage list, after which you can use the Include method family from the 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 take IQueryable as the first argument, and there are strongly typed ones, which is better, then Include(String) .

+15
source

All Articles