0

I have a List object. I am converting this to IQueryable by calling a method AsQueryable().

I need to do sorting on multiple columns say for ex. four columns say col1, col2 col3 & col4 using the expressing tree only. Any one has ideas, how to do that.

What I have tried:

List<employee> obj =  new List<employee> query = obj.AsQueryable<employee>()

var paramterExpression = Expression.Parameter(typeof(Employee));

Expression<func><employee> orderByExpression = (Expression<func><employee>)Expression.Lambda(Expression.PropertyOrField(paramterExpression, "EmpName"), paramterExpression);

the above code is for one column, for the second column some people are saying to use thenby. beyond that nobody says how to do that.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Are you sure you need to build the expression tree by hand? Even if you think you need to do that in the end, I'd suggest writing a simple query first, and then working out what that should map to as an expression tree. It would also help if you'd give a [mcve] - currently your sample code is pretty broken. (Also, see the formatting help for how to format code - you don't need to escape all the angle brackets etc.) – Jon Skeet May 10 '16 at 15:42
  • Beyond that you still use ThenBy, Not using the expression tree, it would be: `obj.OrderBy(...).ThenBy(...).ThenBy(...).ThenBy(...)` – Robert McKee May 10 '16 at 15:55
  • 1
    Possible duplicate of [Dynamic LINQ OrderBy on IEnumerable](http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet) – Ivan Stoev May 10 '16 at 16:27
  • http://stackoverflow.com/search?q=linq+dynamic+orderby – Ivan Stoev May 10 '16 at 16:28

0 Answers0