IQueryable <T> .Include <T,object >>(Expression <Func <T,object >> not working

Jes*_*ham 1 c# iqueryable navigation-properties

我试图使用IQueryable Include方法加载导航属性,但是虽然表达式是正确的我没有得到任何结果

这是代码

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在查询上设置了一个断点.包括并检查数据:

navigationProperties[0] = { n => n.UserStatus }  
navigationProperties[1] = { n => n.PrivilegeLevel }
Run Code Online (Sandbox Code Playgroud)

在单步执行包含行之后,我再次检查了查询值,发现它不包含导航属性

Mar*_*zek 8

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)