首页 新闻 会员 周边

linq 如何根据关键字 sort 和排序方向 order 来动态排序?

0
[已解决问题] 解决于 2017-07-21 10:49
linq 如何根据关键字 string sort  和排序方向 string  order 来动态排序?
order 存储是"asc","desc"之类的
superstar的主页 superstar | 菜鸟二级 | 园豆:261
提问于:2017-05-27 11:28
< >
分享
最佳答案
1

如果9年工作经验确实不该问这样的问题,自己即使不会,百度肯定能找到的。

给你个吧

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Artisan.Framework.Extension
{
    public static class QueryableExtension
    {

        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> query, string propertyName, string order = "asc")
        {
            return _OrderBy<T>(query, propertyName, !(order.ToLower() == "asc"));
        }

        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> query, string propertyName, bool isAsc = true)
        {
            return _OrderBy<T>(query, propertyName, !isAsc);
        }


        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> query, string propertyName)
        {
            return _OrderBy<T>(query, propertyName, false);
        }


        public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> query, string propertyName)
        {
            return _OrderBy<T>(query, propertyName, true);
        }

        static IOrderedQueryable<T> _OrderBy<T>(IQueryable<T> query, string propertyName, bool isDesc)
        {
            string methodname = (isDesc) ? "OrderByDescendingInternal" : "OrderByInternal";

            BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
            var memberProp = typeof(T).GetProperty(propertyName, flag);

            var method = typeof(QueryableExtension).GetMethod(methodname).MakeGenericMethod(typeof(T), memberProp.PropertyType);

            return (IOrderedQueryable<T>)method.Invoke(null, new object[] { query, memberProp });
        }


        public static IOrderedQueryable<T> OrderByInternal<T, TProp>(IQueryable<T> query, PropertyInfo memberProperty)
        {
            return query.OrderBy(_GetLamba<T, TProp>(memberProperty));
        }


        public static IOrderedQueryable<T> OrderByDescendingInternal<T, TProp>(IQueryable<T> query, PropertyInfo memberProperty)
        {
            return query.OrderByDescending(_GetLamba<T, TProp>(memberProperty));
        }
        static Expression<Func<T, TProp>> _GetLamba<T, TProp>(PropertyInfo memberProperty)
        {
            if (memberProperty.PropertyType != typeof(TProp)) throw new Exception();

            var thisArg = Expression.Parameter(typeof(T));
            var lamba = Expression.Lambda<Func<T, TProp>>(Expression.Property(thisArg, memberProperty), thisArg);

            return lamba;
        }
    }
}
奖励园豆:5
Emrys5 | 菜鸟二级 |园豆:223 | 2017-05-27 12:13
其他回答(1)
0

看了一下,你做软件9年了居然,要不还以为这是毕业生问的问题。

你搜索 Linq Dynamic 就知道怎么做了。

爱编程的大叔 | 园豆:30839 (高人七级) | 2017-05-27 11:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册