How do I create an expression tree for run time sorting?

First you need the OrderBy extension method that @Slace wrote here. All credit to Slace for an awesome piece of code and by far the most difficult part of the solution! I made a slight modification for it to work with your specific situation:

public static class QueryableExtensions
{
    public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string sortProperty, ListSortDirection sortOrder)
    {
        var type = typeof(T);
        var property = type.GetProperty(sortProperty);
        var parameter = Expression.Parameter(type, "p");
        var propertyAccess = Expression.MakeMemberAccess(parameter, property);
        var orderByExp = Expression.Lambda(propertyAccess, parameter);
        var typeArguments = new Type[] { type, property.PropertyType };
        var methodName = sortOrder == ListSortDirection.Ascending ? "OrderBy" : "OrderByDescending";
        var resultExp = Expression.Call(typeof(Queryable), methodName, typeArguments, source.Expression, Expression.Quote(orderByExp));

        return source.Provider.CreateQuery<T>(resultExp);
    }
}

Create a method to sort the list. A couple of things to note in the method below:

  1. The List<string> is converted to an IQueryable<string> since Enumerable operators don’t take expression trees.
  2. The method iterates through the list of sort columns in reverse order (assuming you want to give the first item in the list the highest sort priority).
private void PrintVideoList(IEnumerable<string> sortColumns, ListSortDirection sortOrder)
{
    var videos = this.GetVideos();
    var sortedVideos = videos.AsQueryable();

    foreach (var sortColumn in sortColumns.Reverse())
    {
        sortedVideos = sortedVideos.OrderBy(sortColumn, sortOrder);
    }

    // Test the results
    foreach (var video in sortedVideos)
    {
        Console.WriteLine(video.Title);
    }
}

You should then be able to use the method like this:

// These values are entered by the user
var sortColumns = new List<string> { "Width", "Title", "Height" };
var sortOrder = ListSortDirection.Ascending;

// Print the video list base on the user selection
this.PrintVideoList(sortColumns, sortOrder);

Leave a Comment