There are a couple of ways that you can achieve what you're after. First of all, you must realise that you have a fair bit of redundancy in there - you're repeating the ClassBy and Order fields on each item, even though you only need them once. After all, the behaviour would be wildly unpredictable if you were to try to change the sort order/column, throughout the list. Now, supposing that you flatten the data out into a class structure, you can use either Dynamic Linq or reflection to manage the sort. With DLINQ, your query might look something like this:
MyDetailsList list = new MyDetailsList();
list.Populate();
var firstItem = list.First();
var outputList = null;
if (string.Compare(firstItem.OrderBy, "asc", StringComparison.InvariantCultureIgnoreCase) == 0)
{
outputList = list.OrderBy(firstItem.ClassBy);
}
else
{
outputList = list.OrderByDescending(firstItem.ClassBy);
}
Now, if you don't want to use DLINQ, you'd use something like this instead:
MyDetailsList list = new MyDetailsList();
list.Populate();
var firstItem = list.First();
var outputList = null;
var propInfo = list.GetType().GetProperty(firstItem.ClassBy);
if (string.Compare(firstItem.OrderBy, "asc", StringComparison.InvariantCultureIgnoreCase) == 0)
{
outputList = list.OrderBy(x => propInfo.GetValue(x, null));
}
else
{
outputList = list.OrderByDescending(x => propInfo.GetValue(x, null));
}