I have create an orderby expression for my EF generic Repository as following string command = orderByDesc ? "OrderByDescending" : "OrderBy";
var type = typeof(T);
var property = type.GetProperty(orderby);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType },
items.Expression, Expression.Quote(orderByExpression));
items = items.Provider.CreateQuery<T>(resultExpression);
Now I want to create the Expression with 2 columns for ordering and wasn't able to find out something helpful.
Please help me to create an orderby expression with 2 columns.
Ordering by multiple columns in LINQ works by calling OrderBy()
followed by zero or more calls to ThenBy()
. You can't do this using a single call to OrderBy()
.
For example, if you can want to sort by the columns a
and b
, you will have to generate an expression that looks something like:
items.OrderBy(p => p.a).ThenBy(p => p.b)