/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryable"></param> /// <param name="propName"> Property.Property such as User.Name </param> /// <param name="direction"></param> /// <returns></returns> public static IOrderedQueryable <T> OrderBy <T>(this IQueryable <T> queryable, string propName, SortDirection direction) where T : class { var keySelector = ObjectExtension.BuildGetter <T>(propName); string methodName = direction == SortDirection.Ascending ? "OrderBy" : "OrderByDescending"; MethodCallExpression methodCall = Expression.Call(typeof(Queryable), methodName, new Type[] { typeof(T), (keySelector as LambdaExpression).ReturnType }, queryable.Expression, Expression.Quote(keySelector)); return((IOrderedQueryable <T>)queryable.Provider.CreateQuery <T>(methodCall)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="enumerable"></param> /// <param name="propName"> Property.Property such as User.Name </param> /// <param name="direction"></param> /// <returns></returns> public static IOrderedEnumerable <T> ThenBy <T>(this IOrderedEnumerable <T> enumerable, string propName, SortDirection direction) where T : class { LambdaExpression keySelector = ObjectExtension.BuildGetter <T>(propName); string methodName = direction == SortDirection.Ascending ? "ThenBy" : "ThenByDescending"; MethodInfo method = typeof(Enumerable).GetMethods().Single(a => a.Name == methodName && a.IsGenericMethodDefinition && a.GetGenericArguments().Length == 2 && a.GetParameters().Length == 2) .MakeGenericMethod(typeof(T), keySelector.ReturnType); return((IOrderedEnumerable <T>)method.Invoke(null, new object[] { enumerable, keySelector.Compile() })); }