AfterType()之后如何使用Include()? [英] How to use Include() after OfType()?

查看:136
本文介绍了AfterType()之后如何使用Include()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我读了全部 地方,我必须先使用OfType()过滤该集合,然后再包含包含Include()的属性:

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

我不知道如何让Include()工作,因为在我的情况下,Persons是一个DbSet,OfType()返回一个IQueryable,而IQueryable没有定义一个Include()方法。

解决方案

放置:

 使用System.Data.Entity; 

到您的使用列表中,之后您将可以使用 Include 扩展方法family from DbExtensions 类:

  public static IQueryable< T>包括< T,TProperty>(这个IQueryable T源,Expression< Func< T,TProperty>>路径)其中T:class; 
public static IQueryable< T>包括< T>(这个IQueryable< T>源,字符串路径)其中T:class;
public static IQueryable Include(this IQueryable source,string path);

他们接受IQueryable作为第一个参数,也有强类型,更好,然后 Include(String)


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.

解决方案

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

这篇关于AfterType()之后如何使用Include()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆