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

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading