Skip to main content

Posts

Showing posts from July, 2016

linq orderby multiple columns dynamically

So, it's been a while, but I thought I take moment and do my annual blog post ;). I've been playing around with  ASP.NET MVC  and the Linq stuff for  NHibernate  recently. I was in need of an OrderBy extension method that could take a SQL-Like OrderBy string and sort a IQueryable<> or IEnumerable<> collection. I wrote up an implementation that worked, but I just wasn't satisfied with its internals (quite a bit of reflection to get the correct type to construct a LambdaExpression, etc) At any rate, I couldn't leave well enough alone, and, after a bit of Googling, I ran across this  StackOverflow  answer about Dynamic LINQ OrderBy . The extension method wasn't exactly what I was looking for, but that ApplyOrder method is slick, and solved the portion of my implementation that was bothering me. So, I though I would post up my version in case anybody finds it useful. It handles the following inputs: list.OrderBy( "SomeProperty" ); list.Ord