How to use Include() after OfType()?

点点圈 提交于 2019-12-08 17:41:49

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/12085353/how-to-use-include-after-oftype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!