2014-02-22 110 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; 
} 
相关问题