2012-08-23 22 views
7

我正在嘗試加載實體框架模型中派生類的屬性。如何在OfType()之後使用Include()?

我讀all overtheplace我必須包括與性能之前,先過濾鑲有OfType()包括():

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

我不知道怎麼去,其中包括()工作但因爲在我的情況下,Persons是一個DbSet,OfType()返回一個IQueryable,而IQueryable不定義一個Include()方法。

+0

希望這個鏈接幫助http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/2130796d-a2fe-4137-af18-d56480748384 – Boomer

回答

15

廣場這樣的:

using System.Data.Entity; 

到您使用的名單,之後,你將能夠使用Include擴展方法的家人從DbExtensions類:

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作爲第一個參數,也有強類型的,這個更好,那麼Include(String)

+2

啊!謝謝,我認爲這可能是一種擴展方法。我真的希望VS能夠發現名稱空間和/或人們將它們包含在他們的示例中:) –

+0

@XavierPoinas:完全同意你的看法,這讓我第一次感到困惑。 – Dennis

+0

@XavierPoinas我已轉換爲Resharper爲此(其他)的原因。 Resharper將建議使用擴展方法填補智能感知視覺工作室建議中缺失的空白。完全同意你的看法! –

相關問題