{%Pragmatic Coding%} Tips and tricks for software developers.

15Dec/120

Dynamic Linq OrderBy Extension

Few days ago as usualy I working on my project and I made a function where I'm passing field name for sorting  table using Linq stuff. I was in need of an OrderBy extension method that could take a string field parameter in OrderBy and sort a IQueryable<> or IEnumerable<> collection. I was playing around implementation that could work, 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 was exactly what I was looking for.

So, I though I would post up my version in case anybody finds it useful. It handles the following inputs:

I hope this will be helpful for anybody.

Share on social network

Share to Facebook
Share to Google Plus
Share to LiveJournal
Share to MyWorld
Share to Odnoklassniki
Share to Yandex
Tagged as: .NET, C#, LINQ Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.