Search code examples
c#iqueryablenavigation-properties

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


I am trying to load in navigation properties using the IQueryable Include method, however although the expression is correct I am not getting any results

here is the code

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);
        }
    }
}

I put a break point on query.Include and checked the data:

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

after stepping past the include line I checked the queries value again and found that it did not include the nav properties


Solution

  • Include() does not change query instance, it returns new one. You need to assign it back to 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);
            }
        }
    }