• dynamic sorting expression builder


    代码
     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);
            }
        }


  • 相关阅读:
    EnrichPipeline文档
    454ITS数据按barcode和primer分类程序v1.0
    linux系统中,文件的三种特殊权限
    ITS简要分析流程(using Qiime)
    SegmentFault错误汇总
    PCoA主坐标分析
    PCA主成份分析
    软件测试工程师面试题
    我的测试团队管理之道
    如何管理好测试团队
  • 原文地址:https://www.cnblogs.com/wj/p/1847568.html
Copyright © 2020-2023  润新知