叶子的家

~●    ~●  ~●          ~●   ~●~●                           ○
    离成功还很远,距离长着叻,Fighting!
随笔 - 44, 文章 - 1, 评论 - 697, 阅读 - 22万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

dynamic sorting expression builder

Posted on   绿叶  阅读(781)  评论(0编辑  收藏  举报
复制代码
代码
 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);
        }
    }
复制代码


编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示