How to determine if Expression<Func<T, object>> is valid for EntityFramework .Include

Viewed 623

I have a method for retrieving an entity using EntityFramework that accepts an array of the Navigation Properties that should be included:

public virtual T GetSingle(Guid id, params Expression<Func<T, object>>[] navigationProperties)
{
    T item = null;

    IQueryable<T> dbQuery = DBContext.Set<T>();

    Expression<Func<T, bool>> where = t => t.ID == id;
    where = FilterDeleted(where);

    //Apply eager loading
    if (navigationProperties != null)
        foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
            dbQuery = dbQuery.Include<T, object>(navigationProperty);

    item = dbQuery
        .Where(where)//Apply where clause
        .FirstOrDefault(); 

    return item;
}

I'm considering using EntityFramework-Plus to allow filtering on these navigation properties. However, I'm concerned about the complexity/inefficiency of the queries it generates so I want to try using it only on navigation properties that the built in .Include can't handle.

So first I need to determine what is a valid Expression<Func<T, object>> parameter for the .Include method. As far as I know it can only contain a property selector or a .Select. E.G.

t => t.Property;
t => t.Collection.Select(c => c.InnerCollection.Select(ic => ic.Property));

I've barely worked with Expression trees so I'm not sure how to begin taking this on. How would I go about analyzing an Expression<Func<T, object>> to determine if it contains anything other than these such as a .Where or .OrderBy or .Take?

1 Answers
Related