Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to Sql - Repository Pattern - Dynamic OrderBy

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.

like image 755
Martin Avatar asked Jul 10 '26 12:07

Martin


1 Answers

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.

like image 103
Eric King Avatar answered Jul 12 '26 08:07

Eric King



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!