Dynamic Sorting IEnumerable, List

Dynamic Sorting IEnumerable, List

June 10, 2008 10:29 by pradeep.mishra

Earlier I posted about how to dynamically sort the data using extension method see this http://www.noesispedia.com/post/2008/03/21/Linq-to-Sql-Dynamic-Sorting-without-using-Complete-Dynamic-Linq-Libraries.aspx

In this post I will present a quick solution to sort a list of object based on a property of object.

Suppose you have an object like

   1:   
   2:  public class MockObj
   3:  {
   4:  prop a
   5:  prop b
   6:  prop c
   7:  prop d
   8:  }

 

If you are binding above object with gridview (List) and you want to enable sorting in gridview onsorted event use following code to sort

   1:   
   2:  protected void gv_OnSorting(object sender, GridViewSortEventArgs e)
   3:  {
   4:  if (SortDirection == SortDirection.Ascending)
   5:  ((GridView)sender).DataSource = List.OfType().OrderBy(mo=> mo.GetType().GetProperty(e.SortExpression).GetValue(mo, null));
   6:  else
   7:  ((GridView)sender).DataSource = List.OfType().OrderByDescending(mo=> mo.GetType().GetProperty(e.SortExpression).GetValue(mo, null));
   8:  SortDirection = SortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
   9:  ((GridView)sender).DataBind();
  10:  }

Note that sortExpression specified in gridview must match with one of the property on the object.

If you don't want to use this quick but dirty solution you can go for extension methods. For more information see following links

 http://mironabramson.com/blog/post/2008/05/Sorting-a-collection-using-sort-expression-string.aspx

http://www.singingeels.com/Articles/Self_Sorting_GridView_with_LINQ_Expression_Trees.aspx 

Thanks to my friend Joseph for giving this solution and saving time in writing case statements :-) 

Hope this helps! 


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Linq to Sql: Dynamic Sorting without using Complete Dynamic Linq Libraries

Linq to Sql: Dynamic Sorting without using Complete Dynamic Linq Libraries

March 21, 2008 02:39 by pradeep.mishra
This problem may occur while implementing sorting in GridView. If a storedprocedure is being used either dynamic sql can be created or multiple of case statements can be used. However what if you are just using linq queries. Here are the options
  1. Using Dynamic Linq
  2. Some work arround so that linq query can be generated at runtime.
Essentially 2nd approach is the same as that used in 1st one. But if you just want to implement sorting and do not want to digg into Dynamic Linq libraries you can follow the article... Let's assume following method expects sortExpression parameter directly passed by UI layer GridView.
   1:   
   2:  public DataTable GetSomeData(par1, par2...., string sortExpression)
   3:  {
   4:  var query = (//Linq query goes here )
   5:  // We want something like this which is not possible as of now
   6:  var query = (some query) (OrderBy SortExpression)
   7:  }
Here is the extension method you would like to follow...
   1:   
   2:  public DataTable GetSomeData(par1, par2...., string sortExpression)
   3:  {
   4:  var query = (//Linq query goes here )
   5:  // We want something like this which is not possible as of now
   6:  var query = (some query) (OrderBy SortExpression)
   7:  }
   8:  public static Util
   9:  {
  10:  //Thanks to Ernesto for pointing out a small correction in method signature.
  11:  public static  IQueryable<TEntity> OrderBy(this IQueryable<TEntity> source, string sortExpression) where TEntity : class
  12:  {
  13:  var type = typeof(TEntity);
  14:  // Remember that for ascending order GridView just returns the column name and for descending it returns column name followed by DESC keyword
  15:  // Therefore we need to examine the sortExpression and separate out Column Name and order (ASC/DESC)
  16:  string[] expressionParts = sortExpression.Split(' '); // Assuming sortExpression is like [ColoumnName DESC] or [ColumnName]
  17:  string orderByProperty = expressionParts[0];
  18:  string sortDirection = "ASC";
  19:  string methodName = "OrderBy";
  20:  //if sortDirection is descending
  21:  if (expressionParts.Length > 1 && expressionParts[1] == "DESC")
  22:  {
  23:  sortDirection = "Descending";
  24:  methodName += sortDirection; // Add sort direction at the end of Method name
  25:  }
  26:  var property = type.GetProperty(orderByProperty);
  27:  var parameter = Expression.Parameter(type, "p");
  28:  var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  29:  var orderByExp = Expression.Lambda(propertyAccess, parameter);
  30:  MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName,
  31:  new Type[] { type, property.PropertyType },
  32:  source.Expression, Expression.Quote(orderByExp));
  33:  return source.Provider.CreateQuery(resultExp);
  34:  }
  35:  }
Usage will be as of follows...
   1:   
   2:  public DataTable GetSomeData(par1, par2...., string sortExpression)
   3:  {
   4:  var query = (//Linq query goes here )
   5:  // We want something like this which is not possible as of now
   6:  var query = (some query)
   7:  return query.OrderBy(SortExpression).ToDataTable(rec => new object[] { query}));
   8:  }

Again OrderBy is an extension method. Hope this helps!

PS: This is my old post from http://technoesis.wordpress.com. I have imported the comments as well 


Currently rated 3.5 by 2 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5