0

I am using PredicateBuilder to dynamically construct LINQ query as below. I can easily add filter expression usingPredicateBuilder, but I cannot find a way to add dynamic sorting using PredicateBuilder. For example, in code below I would like sort by orderid in an ascending order, but this is decided in a dynamic fashion and not known before hand.

Question : Is it possible to do dynamic sorting using PredicateBuilder, and if yes, then how would that be done? I could not find any suitable method under this class. I could only find System.Linq.Dynamic library suitable for dynamic sorting.

var predicate = PredicateBuilder.True<Orders>();
predicate = predicate.And (o => o.OrderID > 10995);
var ordersFiltered = (from o in Orders select o).Where(predicate);
ordersFiltered.Dump();
Sunil
  • 20,653
  • 28
  • 112
  • 197
  • 1
    Sorting and predicates are two different worlds. – Gert Arnold Nov 12 '15 at 21:49
  • 1
    I had to do something similar where I would not know the amount of sorts and which ones. I built expressions via reflection and chained them. I don't think you can do it via predicates in this case. – Frank J Nov 12 '15 at 21:49
  • I get it now. Since dynamic linq was the goal of PredicateBuilder, I thought it would include all types of linq methods including `Orderby`. But that's not the case. – Sunil Nov 12 '15 at 21:51

1 Answers1

3

PredicateBuilder is there for building predicates, and you're not trying to build a predicate, so no, it won't be able to help you.

Servy
  • 202,030
  • 26
  • 332
  • 449