2014-02-22 107 views
0

我試圖在使用的IQueryable導航屬性加載包括方法,然而雖然表達式正確我沒有得到任何結果IQueryable的<T> .INCLUDE <T, object>>(表達式<Func鍵<T, object>>不工作

這裏是代碼

protected void LoadNavigationProperty(ref IQueryable<T> query, Expression<Func<T, object>>[] navigationProperties) 
{ 
    if ((query != null) && (navigationProperties != null)) 
    { 
     foreach (Expression<Func<T, object>> navigationProperty in navigationProperties) 
     { 
      query.Include<T, object>(navigationProperty); 
     } 
    } 
} 

我把一個破發點上query.Include和檢查數據:

navigationProperties[0] = { n => n.UserStatus } 
navigationProperties[1] = { n => n.PrivilegeLevel } 

步進過去的包含行後,我再次查詢了查詢值,發現它沒有包含導航屬性

回答

6

Include()不變query實例,它返回新的一個。你需要給它分配回query

protected void LoadNavigationProperty(ref IQueryable<T> query, Expression<Func<T, object>>[] navigationProperties) 
{ 
    if ((query != null) && (navigationProperties != null)) 
    { 
     foreach (var navigationProperty in navigationProperties) 
     { 
      query = query.Include<T, object>(navigationProperty); 
     } 
    } 
} 
+0

完整而絕妙的真棒! –

0

馬辛解釋了爲什麼它沒有工作(.Include(做修改查詢),但是我只是想給你做,這樣你就可以使用此方法作爲一種備用方式可以像.Include(.Select(一樣使用擴展方法。

public static IQueryable<T> LoadNavigationProperty(this IQueryable<T> query, Expression<Func<T, object>>[] navigationProperties) 
{ 
    if ((query != null) && (navigationProperties != null)) 
    { 
     foreach (var navigationProperty in navigationProperties) 
     { 
      query = query.Include<T, object>(navigationProperty); 
     } 
    } 

    return query; 
} 
相關問題