/// <summary> /// Orders the IQueryable using the linq sorter. /// </summary> /// <typeparam name="T">The type to order.</typeparam> /// <param name="source">The queryable source.</param> /// <param name="sorter">The linq sorter.</param> /// <returns>The ordered IQueryable.</returns> public static IOrderedQueryable <T> OrderBy <T>(this IQueryable <T> source, LinqSorter <T> sorter) where T : class { Contract.Requires(source != null, "The source must not be null."); Contract.Requires(sorter != null, "The sorter must not be null."); return(source.OrderBy(new List <LinqSorter <T> > { sorter })); }
private static IOrderedQueryable <T> OrderBy <T>(this IQueryable <T> source, LinqSorter <T> sorter, string methodName) where T : class { Contract.Requires(source != null, "The source must not be null."); Contract.Requires(sorter != null, "The sorter must not be null."); Contract.Requires(methodName != null, "The method name must not be null."); var propertyInfo = typeof(T).GetProperties().Where(x => x.Name.ToLower() == sorter.PropertyInfo.Name.ToLower()).FirstOrDefault(); var propertyType = propertyInfo.PropertyType; var xParameter = Expression.Parameter(typeof(T), "x"); var xProperty = Expression.Property(xParameter, propertyInfo); Type delegateType = typeof(Func <,>).MakeGenericType(typeof(T), propertyType); LambdaExpression lambda = Expression.Lambda(delegateType, xProperty, xParameter); var result = typeof(Queryable).GetMethods().Single( method => method.Name == methodName && method.IsGenericMethodDefinition && method.GetGenericArguments().Length == 2 && method.GetParameters().Length == 2) .MakeGenericMethod(typeof(T), propertyType) .Invoke(null, new object[] { source, lambda }); return((IOrderedQueryable <T>)result); }