LINQ Queries with dynamic Order By

前端 未结 3 2164
一个人的身影
一个人的身影 2021-02-14 19:44

I have a query where I need to have ordeby based on a querystring parameter .For example if sortby parameter is price , Query needs to change with price . If its rating than cha

相关标签:
3条回答
  • 2021-02-14 20:25

    Well, you could use a switch statement or something similar:

    IQueryable<Foo> query = ...;
    
    switch (orderByParameter)
    {
        case "price":
            query = query.OrderBy(x => x.Price);
            break;
        case "rating":
            query = query.OrderBy(x => x.Rating);
            break;
        // etc
    }
    

    You could also do it with reflection, but assuming you have a limited number of fields to order by, this is quite possibly the simplest approach.

    0 讨论(0)
  • 2021-02-14 20:26

    If you know exactly which are all the posible parameters that can be used to order, the Jon´s answer is the best one. But if you have an unknown number of parameters you can build the expression dynamically. e.g:

    class Program
    {
        static void Main(string[] args)
        {
            var people = new[]{
                new Person { Name = "David", Age = 40 },
                new Person { Name = "Maria", Age = 12 },
                new Person { Name = "Lucas", Age = 45 }
            }.AsQueryable();
    
            foreach (var p in people.OrderBy("Age"))
            {
                Console.Write(p.Name);
            }
        }
    
        class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
    
    static class IQueryableExtensions
    {
        public static IQueryable<T> OrderBy<T>(this IQueryable<T> items, string propertyName)
        {
            var typeOfT = typeof(T);
            var parameter = Expression.Parameter(typeOfT, "parameter");
            var propertyType = typeOfT.GetProperty(propertyName).PropertyType;
            var propertyAccess = Expression.PropertyOrField(parameter, propertyName);
            var orderExpression = Expression.Lambda(propertyAccess, parameter);
    
            var expression = Expression.Call(typeof(Queryable), "OrderBy", new Type[] { typeOfT, propertyType }, items.Expression, Expression.Quote(orderExpression));
            return items.Provider.CreateQuery<T>(expression);
        }        
    }
    
    0 讨论(0)
  • 2021-02-14 20:44

    Expanding on @lontivero answer.

    If you want to do dynamic sorting of multiple items both ascending and descending you can do something similar to this below. Adds the OrderByDescending, ThenBy, ThenByDescending methods

    static class IQueryableExtensions
     {
         public static IQueryable<T> OrderBy<T>(this IQueryable<T> items, string propertyName)
         {
             var typeOfT = typeof(T);
             var parameter = Expression.Parameter(typeOfT, "parameter");
             var propertyType = typeOfT.GetProperty(propertyName).PropertyType;
             var propertyAccess = Expression.PropertyOrField(parameter, propertyName);
             var orderExpression = Expression.Lambda(propertyAccess, parameter);
    
             var expression = Expression.Call(typeof(Queryable), "OrderBy", new Type[] { typeOfT, propertyType }, items.Expression, Expression.Quote(orderExpression));
             return items.Provider.CreateQuery<T>(expression);
         }
    
         public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> items, string propertyName)
         {
             var typeOfT = typeof(T);
             var parameter = Expression.Parameter(typeOfT, "parameter");
             var propertyType = typeOfT.GetProperty(propertyName).PropertyType;
             var propertyAccess = Expression.PropertyOrField(parameter, propertyName);
             var orderExpression = Expression.Lambda(propertyAccess, parameter);
    
             var expression = Expression.Call(typeof(Queryable), "OrderByDescending", new Type[] { typeOfT, propertyType }, items.Expression, Expression.Quote(orderExpression));
             return items.Provider.CreateQuery<T>(expression);
         }
    
         public static IQueryable<T> ThenBy<T>(this IQueryable<T> items, string propertyName)
         {
             var typeOfT = typeof(T);
             var parameter = Expression.Parameter(typeOfT, "parameter");
             var propertyType = typeOfT.GetProperty(propertyName).PropertyType;
             var propertyAccess = Expression.PropertyOrField(parameter, propertyName);
             var orderExpression = Expression.Lambda(propertyAccess, parameter);
    
             var expression = Expression.Call(typeof(Queryable), "ThenBy", new Type[] { typeOfT, propertyType }, items.Expression, Expression.Quote(orderExpression));
             return items.Provider.CreateQuery<T>(expression);
         }
    
         public static IQueryable<T> ThenByDescending<T>(this IQueryable<T> items, string propertyName)
         {
             var typeOfT = typeof(T);
             var parameter = Expression.Parameter(typeOfT, "parameter");
             var propertyType = typeOfT.GetProperty(propertyName).PropertyType;
             var propertyAccess = Expression.PropertyOrField(parameter, propertyName);
             var orderExpression = Expression.Lambda(propertyAccess, parameter);
    
             var expression = Expression.Call(typeof(Queryable), "ThenByDescending", new Type[] { typeOfT, propertyType }, items.Expression, Expression.Quote(orderExpression));
             return items.Provider.CreateQuery<T>(expression);
         }
     }
    
    0 讨论(0)
提交回复
热议问题