Those of us in the C# realm have become so used to LINQ it’s difficult to remember the problems of yesteryear and how we used to solve them. The reason is that almost all of the time, LINQ takes that work we used to do and abstracts a lot of the pain into a query syntax.

But there are some problems things that used to be more intuitive. The dinosaurs among us remember writing code like this:

void SortRecords(DataTable data, string column, string direction) { 
	data.DefaultView.Sort = String.Join("{0} {1}", column, direction); 
} 


In a typical scenario you might have a user click on a column, in some interface and pass that to your SortRecords method to change the way the data is displayed. In the present day, using LINQ to sort is trivial but because LINQ takes expressions as parameters for the typically used OrderBy and OrderByDescending, using a string isn’t as straight forward of a process. What’s a path around this problem?

First, let’s demonstrate the problem. Assume I have a class Person with two properties, FirstName and LastName. Let’s further assume that although an instance of Person is useful, we often have lists of Person that we occasionally find ourselves having to sort.

var people = new List() { 
        new Person(){ FirstName = "Stan", LastName = "Lee"}, 
        new Person(){ FirstName = "Jack", LastName = "Kirby"}, 
        new Person(){ FirstName = "Alex", LastName = "Ross"},
        new Person(){ FirstName = "Adi", LastName = "Granov"}
};


A typical sorting expression in LINQ takes an expression, not a string that could be passed as a parameter.

var result = people.OrderBy(p => p.LastName); // not a string


This creates a problem if you try to generalize sorting into something more reusable like a method or a function. Sure, you could pass an expression, but passing those expressions from the UI can be a little bit tricky. So what course of action?

My solution is to use a dispatch table. I first saw the concept back in my days hacking perl but it works in C# with delegates. Here is an example of a dispatch table using strings as keys for our anonymous delegates representing the Linq expression that we would use to sort:

Dictionary<string, Func<Person, object>> sortLookups = new Dictionary<string, Func<Person, object>>() { 
    {"FirstName", new Func<Person, object>(p => p.FirstName)},
    {"LastName", new Func<Person, object>(p => p.LastName)}
};


Now the task of sorting by string is simple because that can be used as the key to reference the correct delegate that would be used as the sort expression. This anonymous method demonstrates the concept:

IEnumerable<Person> SortThem(string fieldName, IEnumerable<Person> people) {
    Dictionary<string, Func<Person, object>> sortLookups = new Dictionary<string, Func<Person, object>>() { 
        {"FirstName", new Func<Person, object>(p => p.FirstName)},
        {"LastName", new Func<Person, object>(p => p.LastName)}
    };
    // Q.E.D. really
    return people.OrderBy(sortLookups[fieldName]);
}


Here is an example of a call to our SortThem method:

>
var people = new List() { 
        new Person(){ FirstName = "Stan", LastName = "Lee"}, 
        new Person(){ FirstName = "Jack", LastName = "Kirby"}, 
        new Person(){ FirstName = "Alex", LastName = "Ross"},
        new Person(){ FirstName = "Adi", LastName = "Granov"}
};
var sortedByLast = SortThem("LastName", people);


Dispatch tables make stringifying method parameters and dealing with LINQ much more palatable since LINQ is all about dealing with expressions. This is just the tip of the iceberg. Once you know how to match up anonymous methods into dispatch tables watch out! You’ll start seeing applications of the technique everywhere!