如何使用include()OfType后的()?如何使用、include、OfType

2023-09-04 08:58:21 作者:妆ζ

我想派生类的实体框架模型急于负载性能。

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

我看了各地 的 的地方,我必须先过滤镶有OfType()包括属性之前,包括():

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

我不知道怎么弄,包括()的工作,但因为在我的情况下,人是一个DbSet,OfType()返回一个IQueryable和IQueryable的没有定义包括()方法。

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.

推荐答案

将这样的:

using System.Data.Entity;

到你使用的名单,之后,你就可以使用包含扩展方法家族的 DbExtensions 类:

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

他们接受的IQueryable作为第一个参数,并有强类型的人,也是如此,这是更好的,那么包含(字符串)