I’m using PredicateBuilder and LINQKit from Albahari to construct a LINQ query that has optional parameters, optional OrderBy, optional Skip() and optional Take().
PredicateBuilder makes it easy to build the optional parameters, but I’m having problems with the other stuff. To simplify the issue:
This works
var query = this.MyContext.Things.AsExpandable.Where(predicate).OrderBy(s => s.Name);
return query.ToList();
I want to do something like this, so I can wrap the OrderBy in an if. It doesn’t work however, the results are not in name order.
var query = this.MyContext.Things.AsExpandable().Where(predicate);
query.OrderBy(s => s.Name);
return query.ToList();
Can someone point me in the right direction? I’m an NHibernate convert and still getting to grips with LINQ.
You are missing a re-assignment to your
queryvariable –OrderBy()(same as all other standard query operators) returns a newIQueryable<T>: