If you’re familiar with Linq, you have undoubtedly used the popular “OrderBy” extension method. Unfortunately, this method does not accept a string value with the column name.

var data = from i in db.tablename
select i;

repeatername.datasource = data.OrderBy(i=>i.columnname); //this works

repeatername.datasource = data.OrderBy("columnname"); //this does not

To resolve, you can add the following small class or just the method to a generic DLL you use in your web application projects or create a new class or project for your extension methods.

Kudos to R. Prestol for help researching this one.

using System.Linq;
using System.Linq.Expressions;
using System;

namespace YourAppName.Web
{
    public static class extensionmethods
    {
        public static IQueryable<T> OrderByField<T>(this IQueryable<T> q, string SortField, bool Ascending)
        {
            var param = Expression.Parameter(typeof(T), "p");
            var prop = Expression.Property(param, SortField);
            var exp = Expression.Lambda(prop, param);
            string method = Ascending ? "OrderBy" : "OrderByDescending";
            Type[] types = new Type[] { q.ElementType, exp.Body.Type };
            var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
            return q.Provider.CreateQuery<T>(mce);
        }
    }
}

Returning to our first code snippet, you can now do:

repeater.datasource = data.OrderByField("columnname");

Since this is an extension method, and exists in a class within the same namespace as your project or other referenced assembly, you can call the method directly from the IQueryable Linq data object without requiring any other inherits; this works similar to override functionality.

Read up about extension methods on MSDN or Google for other cool tricks you can do. Enjoy. 😉

References
Extension Methods (MSDN), http://msdn.microsoft.com/en-us/library/bb383977.aspx

Ronnie Diaz Avatar

Published by

14 responses to “OrderBy Column Name as String using Linq C# .Net Dynamic Sorting of Anonymous Types”

  1. Joby Avatar
    Joby

    This is absolutely great. Thank you so much.

  2. Bob King Avatar
    Bob King

    fantastic; I just couldn’t get the sidx+sord method to work when trying to sort jqGrid data, and this did the trick elegantly. Can’t believe it isn’t native in Linq

  3. mimi Avatar
    mimi

    That’s what I really looking for . thank you!

  4. Robin Avatar
    Robin

    fantastic – saved me hours of fruitless searcvhing

  5. zulee Avatar
    zulee

    Great!!! After two days You solved my problem!
    Thanx man!

  6. […] used this link for reference, which is used for sorting of single column by column […]

  7. Guilherme Avatar
    Guilherme

    Great!!!! Thanks a lot man, great solution for a big problem!

  8. Hossein Avatar
    Hossein

    Great! thanks a lot , works great on our system.

  9. Mel Avatar
    Mel

    Fantastic! after three days of searching finally I found the answer, thanks you so much!

  10. MP Avatar
    MP

    Thanks a lot! Exactly what I needed

  11. Bu Konde Avatar

    Thank you! you saved my hours 😉

  12. Dave Avatar
    Dave

    Thanks, only thing I needed to do to get it to work in LINQ pad was change:
    var prop = Expression.Property(param, SortField);
    To
    var fld = Expression.Field(param, SortField);

  13. Chen Avatar
    Chen

    Hi,

    How do I do it with multiple columns?

    Thanks,
    Chen

  14. Hemant Avatar

    Amazing!!! Thanks for sharing.

Leave a comment