I once stuck on a requirement where I needed to write a code which is flexible and generic enough to sort any type of collection and also the property based on which it has to be sorted will be dynamic. After trying many options I discovered myself how easy it is to hit this requirement using Lamda Expression.
Below code will do this for you.
public class Utility<T> { /// <Summary> /// Gets the sorted list. /// </Summary> /// <param name="source" />The source. /// <param name="sortColumn" />The sort column. /// <param name="sortDirection" />The sort direction. /// <The sorted list. /> private List<T> GetSortedList(List<T> source, string sortColumn, SortDirection sortDirection) { // Prepare the dynamic sort expression var paramExp = Expression.Parameter(typeof(T), typeof(T).ToString()); Expression propConvExp = Expression.Convert(Expression.Property(paramExp, sortColumn), typeof(object)); var sortExp = Expression.Lambda>(propConvExp, paramExp); if (sortDirection == SortDirection.Ascending) { return source.AsQueryable().OrderBy(sortExp).ToList(); } else { return source.AsQueryable().OrderByDescending(sortExp).ToList(); } } }
We will call this method as below
List<Employee> sortedEmployees = new Utility<Employee>().GetSortedList(employeeList, "City", SortDirection.Ascending);
First we create the parameter expression for the generic type and on the further two statements we are promting the expression to convert the dynamic property based on which we need to sort to a understandable format of lambda. Then we go ahead and use the sort of generic list.
Aren’t you loving lambda expressions?
Sort Generic List Using Lambda Expression For Dynamic Type And Dynamic Property
I'm unable to use your code. What is the "Expression" is it lambda expression or some type. If it is a type then what is its namespace???
Looks to be a gud post but useless for me so far. It always says "Expression does not exists in the current context.
Kashif use System.Linq.Expressions
Iam m getting error in that statement :try specifying the arguments explicitly..
return source.AsQueryable().OrderBy(sortExp).ToList();
i added namespaces system.collections.generic;system.Linq.Expressions;
Could any one help me regarding this..
Sruthi Please mail me your code
hi jebarson,
even i m getting the same error..can you help me out here?
please paste / mail me your code.
Thanks Jebarson for writing this article, which is very helpful. I found one of the correction for the above code, where I always get compilation error in OrderBy statements.
Changed the code from
var sortExp = Expression.Lambda>(propConvExp, paramExp);
To below
Expression<Func> sortExp = Expression.Lambda<Func>(propConvExp, paramExp);
for the sortExp for the below OrderBy should be called with sortExp.Compile()
From this
return source.AsQueryable().OrderBy(sortExp).ToList();
return source.AsQueryable().OrderByDescending(sortExp).ToList();
To
return source.AsQueryable().OrderBy(sortExpCompile()).ToList();
return source.AsQueryable().OrderByDescending(sortExpCompile()).ToList();
I hope this will solve the problem with OrderBy while providing the sorting expressions for Generic implementation.
The below code works fine, there is missing “<Func>” item in lambda expression
var paramExp = Expression.Parameter(typeof(T), typeof(T).ToString());
Expression propConvExp = Expression.Convert(Expression.Property(paramExp, sortColumn), typeof(object));
var sortExpression = Expression.Lambda<Func>(propConvExp, paramExp);
if (sortDirection == SortDirection.Ascending)
{
return source.AsQueryable().OrderBy(sortExpression).ToList();
}
else
{
return source.AsQueryable().OrderByDescending(sortExpression).ToList();
}