Joseph Michael Pesch
VP Programming

LINQ Dynamic Order By on IQuerable Object

by 2. November 2009 23:24

private IQueryable<ClientInfo> OrderBy(IQueryable obj){

  ParameterExpression param = Expression.Parameter(typeof(ClientInfo), string.Empty);

  MemberExpression property = Expression.PropertyOrField(param, this.SortExpressionLabel.Text);  

  LambdaExpression sort = Expression.Lambda(property, param); 

  string methodName = "OrderBy"; 

  if (SortDirectionLabel.Text == "DESC")  methodName += "Descending"; 

  MethodCallExpression call = Expression.Call(typeof(Queryable), methodName, new[] { typeof(ClientInfo), property.Type },      obj.Expression, Expression.Quote(sort)); 

  return obj.Provider.CreateQuery<ClientInfo>(call);

}

Tags: ,

LINQ to SQL

Comments are closed