private IQueryable _BuildOrderByQueryable(IQueryable baseQueryable, JsonQueryOrderBy jsonQueryOrderBy, bool isFirst) { var typeParameter = Expression.Parameter(baseQueryable.ElementType); var orderByExpression = _GetOrderByExpression(typeParameter, jsonQueryOrderBy); MethodInfo methodInfo; if (isFirst) { if (jsonQueryOrderBy.Direction == JsonQueryOrderDirection.Asc) { methodInfo = new Func <IQueryable <object>, Expression <Func <object, object> >, IOrderedQueryable <object> >(Queryable.OrderBy) .GetMethodInfo(); } else { methodInfo = new Func <IQueryable <object>, Expression <Func <object, object> >, IOrderedQueryable <object> >(Queryable.OrderByDescending) .GetMethodInfo(); } } else { if (jsonQueryOrderBy.Direction == JsonQueryOrderDirection.Asc) { methodInfo = new Func <IOrderedQueryable <object>, Expression <Func <object, object> >, IOrderedQueryable <object> >(Queryable.ThenBy) .GetMethodInfo(); } else { methodInfo = new Func <IOrderedQueryable <object>, Expression <Func <object, object> >, IOrderedQueryable <object> >(Queryable.ThenByDescending) .GetMethodInfo(); } } var method = methodInfo .GetGenericMethodDefinition() .MakeGenericMethod(baseQueryable.ElementType, orderByExpression.ReturnType); return(baseQueryable.Provider.CreateQuery( Expression.Call( instance: null, method: method, arg0: baseQueryable.Expression, arg1: Expression.Quote(orderByExpression)))); }
private LambdaExpression _GetOrderByExpression(ParameterExpression typeParameter, JsonQueryOrderBy jsonQueryOrderBy) { var memberExpression = _GetMemberExpression(typeParameter, jsonQueryOrderBy.Selector); var lambdaExpression = Expression.Lambda( body: memberExpression, typeParameter); return(lambdaExpression); }