Say I have an instance of IQueryable. How can I find out by what parameters it was ordered?
Here's what the OrderBy () method looks like (as a link):
public static IOrderedQueryable<T> OrderBy<T, TKey>( this IQueryable<T> source, Expression<Func<T, TKey>> keySelector) { return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>( Expression.Call(null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod( new Type[] { typeof(T), typeof(TKey) } ), new Expression[] { source.Expression, Expression.Quote(keySelector) } ) ); }
Hint from Matt Warren:
All queries (even IOrderedQueryable) contain expression trees underlying them that encode the activity that they represent. You should find, using the IQueryable.Expression property, an expression-expression of the node method representing a call to the Queryable.OrderBy method with the actual arguments specified. You can decode the expression used for ordering from the keySelector argument. Take a look at the IOrderedQueryable object instance in the debugger to see what I mean.
source share