Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Queries with dynamic Order By

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 change query to sort by rating .

I know PredicateBuilder can do And and OR stuff but how do I make a dynamic ordeby linq query .

like image 705
Pit Digger Avatar asked Oct 21 '11 22:10

Pit Digger


3 Answers

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.

like image 149
Jon Skeet Avatar answered Nov 07 '22 20:11

Jon Skeet


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);
    }        
}
like image 32
lontivero Avatar answered Nov 07 '22 20:11

lontivero


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);
     }
 }
like image 24
hewstone Avatar answered Nov 07 '22 20:11

hewstone