LINQ Queries with dynamic Order By

前端 未结 3 2163
一个人的身影
一个人的身影 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: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 OrderBy(this IQueryable 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(expression);
         }
    
         public static IQueryable OrderByDescending(this IQueryable 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(expression);
         }
    
         public static IQueryable ThenBy(this IQueryable 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(expression);
         }
    
         public static IQueryable ThenByDescending(this IQueryable 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(expression);
         }
     }
    

提交回复
热议问题