views:

652

answers:

3

Ok, I found this, which will allow me to do this:

public IList<Item> GetItems(string orderbyColumn)
{
    return _repository.GetItems().OrderBy(orderByColumn).ToList();
}

Is this the best way to do "dynamic" ordering? I want to be able to pass the column name as a string (and the sort direction) to my Service, and have it order the correct way.

+6  A: 

That's certainly a viable way of doing dynamic sorting. Ch00k provided another option in his answer to this question about "Strongly typed dynamic Linq sorting". I personally prefer Ch00k's method, as there's some compile-time checking involved and there's very little extra code involved.

Eric King
And his answer (Ch00k) will be refactoring-safe as well, do a rename of a property, and the code updates, with the property name in a string, that won't happen!
Lasse V. Karlsen
+3  A: 

If you've already decided that it must be a string, then your options are somewhat limited. The Dynamic LINQ library would indeed do the job, or if you want t know how it all works, look at this previous answer which builds an Expression from the string at runtime.

At the moment the code only accepts a single member and has separate methods for ascending / descending, but from this example it should be fairly simple to pass a more complex string and split it; essentially as:

IQueryable<T> query = ...
string[] portions = orderBy.Split(' '); // split on space, arbitrarily
if(portions.Length == 0) throw new ArgumentException();
IOrderedQueryable<T> orderedQuery = query.OrderBy(portions[0]);
for(int i = 1 ; i < portions.Length ; i++) { // note we already did the zeroth
    orderedQuery = orderedQuery.ThenBy(portions[i]);
}
return orderedQuery;
Marc Gravell
It doesn't have to be a string, but how would I pass the sort column from the controller to the Repository/Service?
Martin
If not a string, then the only other sensible choice would be a lambda Expression, or an object that encapsulates more than one lambda expression. Or just use the existing OrderBy/ThenBy.
Marc Gravell
+1  A: 

If you're just after dynamic sorting without the full Dynamic-Linq stuff you can check out a post I wrote about this a while back: click

ray2k