Linq Extention - OrderBy

public static class LinqExtentions { public static IQueryable<T> OrderBy<T>(this IQueryable<T> query, string sortColumn, string direction) { if (string.IsNullOrEmpty(sortColumn)) return query; string methodName = string.Format("OrderBy{0}", direction.ToLower() == "asc" ? "" : "descending"); ParameterExpression parameter = Expression.Parameter(query.ElementType, "p"); MemberExpression memberAccess = null; foreach (var property in sortColumn.Split('.')) memberAccess = MemberExpression.Property(memberAccess ?? (parameter as Expression), property); LambdaExpression orderByLambda = Expression.Lambda(memberAccess, parameter); MethodCallExpression result = Expression.Call( typeof (Queryable), methodName, new[] {query.ElementType, memberAccess.Type}, query.Expression, Expression.Quote(orderByLambda)); return query.Provider.CreateQuery<T>(result); } }
Extension to help order a IEnumerable<T> row

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.