Using Entity Framework 4, I'm trying to implement dynamic sorting based on a collection of member names. Basically, the user can select fields to sort and the order of the sorting. I've looked at expression tree examples and can't piece this together. Here are some details:
Collection of column names:
public List<string> sortColumns;
sortColumns = new List<string>();
/// Example subset of video fields. The collection will vary.
sortColumns.Add("Width");
sortColumns.Add("Height");
sortColumns.Add("Duration");
sortColumns.Add("Title");
The video class is defined as follows:
public class Video
{
public string Title { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public float Duration { get; set; }
public string Filename { get; set; }
public DateTime DateCreated { get; set; }
.
.
.
}
public List<Video> Videos;
What I would like to do is enumerate through the sortColumns collection to build an expression tree at run time. Also, the user could specify either ascending or descending sorting and the expression tree should handle either.
I tried the Dynamic LINQ library for VS 2008, but it doesn't appear to work in VS 2010. (I could be doing something wrong.)
The bottom line is I need an expression tree to dynamically sort the Videos collection based on user input. Any help would be appreciated.