Q:

Extract the Linq query from an IQueryable?

I've inherited an app and am trying to debug why a field is missing from the results of one of the queries running through Entity Framework 6 that was written before me.
The original developer created a system that takes in an entity context, a filter Lambda expression, order by limitations and included properties.  In order for me to play with the query to see what is missing, I want to extract the Linq/Lambda query out of the IQueryable that is put together at the end of the function in order to execute it in Linqpad.
Ideally, what I would like is to see what kind of "select x from XTable.Include("SomeOtherTable").Where(Predicates)" is pieced together?  I know how to see the SQL that is generated, but that doesn't help much in figuring out the parameters that need changing.
Here's the function.  I basicly need the end linq statement in one of the two return statements before applying the Skip/Take/ToArray/ToList.
    protected override IEnumerable<T> GetPagedEntity(MyDBContext entityContext, int skip, int take, System.Linq.Expressions.Expression<Func<T, bool>> filter, string orderBy, string includeProperties, out int count)
    {
        IQueryable<T> query = entityContext.Set<T>();
        string[] orderby = !string.IsNullOrEmpty(orderBy) ? orderBy.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) : null;
        if (filter != null)
        {
            query = query.Where(filter);
        }
        count = query.Count();
        if (!string.IsNullOrEmpty(includeProperties))
        {
            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }
        }
        if (!string.IsNullOrWhiteSpace(orderBy) && orderby != null && orderby.Length > 0)
        {
            for (int i = 0; i < orderby.Length; i++)
            {
                if (i == 0)
                {
                    if (orderby[i].ToLower().Contains(" desc"))
                    {
                        query = FilterExpressionUtil.OrderByDescending(query, orderby[i].Trim().Split(' ')[0]);
                    }
                    else
                    {
                        query = FilterExpressionUtil.OrderBy(query, orderby[i].Trim());
                    }
                }
                else
                {
                    if (orderby[i].ToLower().Contains(" desc"))
                    {
                        query = FilterExpressionUtil.ThenByDescending(query as IOrderedQueryable<T>, orderby[i].Trim().Split(' ')[0]);
                    }
                    else
                    {
                        query = FilterExpressionUtil.ThenBy(query as IOrderedQueryable<T>, orderby[i].Trim());
                    }
                }
            }
            return query.Skip(skip).Take(take).ToArray().ToList();
        }
        else
        {
            return query.OrderBy(a => 1).Skip(skip).Take(take).ToArray().ToList();
        }
    }

A:

From what I can see, there's really not much extraction to do because a lot of it is being passed in.
IQueryable<T> query = entityContext.Set<T>();

is giving you the entire DBSet of the T type you are passing in. (I would recommend putting a where T : DbSet type constraint on it, not sure if that is the exact syntax, but make sure you can only pass in a type that the entityContext (first param) knows about.
if (filter != null)
        {
            query = query.Where(filter);
        }

There is no "Where" clause in this code, it is being passed into the function with the System.Linq.Expressions.Expression<Func<T, bool>> filter parameter.
if (!string.IsNullOrEmpty(includeProperties))
        {
            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }
        }

There is no "Include" clauses in this code either, it is being passed into the function. 
The next big block is handling the "OrderBy" stuff and is returning IEnumerable<T> with Skip(skip).Take(take).ToArray().ToList(); tacked onto the end.

The answer to your question will be different for each different call to this function.

For Example:
GetPagedEntity<User>(
entityContext:declaredContext, 
skip:20, 
take:40, 
filter: (u => u.IsActive == true), 
orderBy: "FirstName,LastName", 
includeProperties: "table1,table2", 
out count:declaredInt
)

will look something like this:
declaredContext.Set<User>().Where(u => u.IsActive == true).Include("table1").Include("table2").OrderBy(u => u.FirstName).ThenBy(u => u.LastName).Skip(20).Take(40).ToArray().ToList();

And the output parameter count will return you the count BEFORE the Take method takes the specified amount. Hope this helps, sorry if my syntax is a little off.

