Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Include() after OfType()?

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.

like image 703
Xavier Poinas Avatar asked Aug 23 '12 05:08

Xavier Poinas


1 Answers

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).

like image 152
Dennis Avatar answered Oct 04 '22 04:10

Dennis