代码
public static class Paging
{
public interface ISortbyBuilder<TSource>
{
IQueryable<TSource> SortBy(IQueryable<TSource> obj, LambdaExpression keySelector, bool asc);
}
public class SortbyBuilder<TSource, TKey> : ISortbyBuilder<TSource>
{
public IQueryable<TSource> SortBy(IQueryable<TSource> obj, LambdaExpression keySelector, bool asc)
{
var castedKeySelector = (Expression<Func<TSource, TKey>>) keySelector;
return asc ? obj.OrderBy(castedKeySelector) : obj.OrderByDescending(castedKeySelector);
}
}
public class SortbyBuilderCache<TSource>
{
private static readonly IDictionary<Type, ISortbyBuilder<TSource>> Cache = new Dictionary<Type, ISortbyBuilder<TSource>>();
public static IQueryable<TSource> SortBy(IQueryable<TSource> obj, LambdaExpression keySelector, bool asc)
{
// TODO: I told you this is magic!
var selectorReturnType = keySelector.Body.Type;
ISortbyBuilder<TSource> sortbyBuilder;
if (!Cache.TryGetValue(selectorReturnType, out sortbyBuilder))
{
var typeArguments = new[] { typeof(TSource), selectorReturnType };
var builderType = typeof(SortbyBuilder<,>).MakeGenericType(typeArguments);
sortbyBuilder = (ISortbyBuilder<TSource>)Activator.CreateInstance(builderType);
}
return sortbyBuilder.SortBy(obj, keySelector, asc);
}
}
public static IPagination<T> AsPagination<T>(this IQueryable<T> obj, int pageNumber, int pageSize, string sortExpression)
{
sortExpression = sortExpression ?? string.Empty;
var asc = !sortExpression.Contains("DESC");
var sortKey = sortExpression.Split(' ')[0];
if (string.IsNullOrEmpty(sortKey) || TypeDescriptor.GetProperties(typeof (T)).Find(sortKey, false) == null)
{
return obj.AsPagination(pageNumber, pageSize);
}
// TODO: magic
var parameterExpression = Expression.Parameter(typeof (T));
var propertyExpression = Expression.Property(parameterExpression, sortKey);
LambdaExpression lambdaExpression;
if (typeof (IEnumerationWrapper).IsAssignableFrom(propertyExpression.Type))
{
var valueExpression = Expression.Property(propertyExpression, "Value");
lambdaExpression = Expression.Lambda(valueExpression, new[] {parameterExpression});
}
else
{
lambdaExpression = Expression.Lambda(propertyExpression, new[] {parameterExpression});
}
return SortbyBuilderCache<T>.SortBy(obj, lambdaExpression, asc).AsPagination(pageNumber, pageSize);
}
public static IPagination<T> AsPagination<T, TKey>(this IQueryable<T> obj, int pageNumber, int pageSize, Expression<Func<T, TKey>> keySelector, bool asc)
{
return asc ? obj.OrderBy(keySelector).AsPagination(pageNumber, pageSize) : obj.OrderByDescending(keySelector).AsPagination(pageNumber, pageSize);
}
}
{
public interface ISortbyBuilder<TSource>
{
IQueryable<TSource> SortBy(IQueryable<TSource> obj, LambdaExpression keySelector, bool asc);
}
public class SortbyBuilder<TSource, TKey> : ISortbyBuilder<TSource>
{
public IQueryable<TSource> SortBy(IQueryable<TSource> obj, LambdaExpression keySelector, bool asc)
{
var castedKeySelector = (Expression<Func<TSource, TKey>>) keySelector;
return asc ? obj.OrderBy(castedKeySelector) : obj.OrderByDescending(castedKeySelector);
}
}
public class SortbyBuilderCache<TSource>
{
private static readonly IDictionary<Type, ISortbyBuilder<TSource>> Cache = new Dictionary<Type, ISortbyBuilder<TSource>>();
public static IQueryable<TSource> SortBy(IQueryable<TSource> obj, LambdaExpression keySelector, bool asc)
{
// TODO: I told you this is magic!
var selectorReturnType = keySelector.Body.Type;
ISortbyBuilder<TSource> sortbyBuilder;
if (!Cache.TryGetValue(selectorReturnType, out sortbyBuilder))
{
var typeArguments = new[] { typeof(TSource), selectorReturnType };
var builderType = typeof(SortbyBuilder<,>).MakeGenericType(typeArguments);
sortbyBuilder = (ISortbyBuilder<TSource>)Activator.CreateInstance(builderType);
}
return sortbyBuilder.SortBy(obj, keySelector, asc);
}
}
public static IPagination<T> AsPagination<T>(this IQueryable<T> obj, int pageNumber, int pageSize, string sortExpression)
{
sortExpression = sortExpression ?? string.Empty;
var asc = !sortExpression.Contains("DESC");
var sortKey = sortExpression.Split(' ')[0];
if (string.IsNullOrEmpty(sortKey) || TypeDescriptor.GetProperties(typeof (T)).Find(sortKey, false) == null)
{
return obj.AsPagination(pageNumber, pageSize);
}
// TODO: magic
var parameterExpression = Expression.Parameter(typeof (T));
var propertyExpression = Expression.Property(parameterExpression, sortKey);
LambdaExpression lambdaExpression;
if (typeof (IEnumerationWrapper).IsAssignableFrom(propertyExpression.Type))
{
var valueExpression = Expression.Property(propertyExpression, "Value");
lambdaExpression = Expression.Lambda(valueExpression, new[] {parameterExpression});
}
else
{
lambdaExpression = Expression.Lambda(propertyExpression, new[] {parameterExpression});
}
return SortbyBuilderCache<T>.SortBy(obj, lambdaExpression, asc).AsPagination(pageNumber, pageSize);
}
public static IPagination<T> AsPagination<T, TKey>(this IQueryable<T> obj, int pageNumber, int pageSize, Expression<Func<T, TKey>> keySelector, bool asc)
{
return asc ? obj.OrderBy(keySelector).AsPagination(pageNumber, pageSize) : obj.OrderByDescending(keySelector).AsPagination(pageNumber, pageSize);
}
}