How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?

We did something similar (not 100% the same, but similar) in a LINQ to SQL project. Here's the code: public static IQueryable OrderBy(this IQueryable source, string ordering, params object values) { var type = typeof(T); var property = type. GetProperty(ordering); var parameter = Expression.

Parameter(type, "p"); var propertyAccess = Expression. MakeMemberAccess(parameter, property); var orderByExp = Expression. Lambda(propertyAccess, parameter); MethodCallExpression resultExp = Expression.

Call(typeof(Queryable), "OrderBy", new Type { type, property. PropertyType }, source. Expression, Expression.

Quote(orderByExp)); return source.Provider. CreateQuery(resultExp); } We didn't actually use a generic, we had a known class, but it should work on a generic (I've put the generic placeholder where it should be).

Heh no prob, I can't assign the answer to myself anyways :) – JTew Nov 21 '08 at 3:47 for descending order, pass in "OrderByDescending" instead of "OrderBy" MethodCallExpression resultExp = Expression. Call(typeof(Queryable), "OrderByDescending", ... – Garry English Jul 26 at 19:18.

You can also use Dynamic Linq Info here weblogs.asp.net/scottgu/archive/2008/01/... C# download here msdn.microsoft.com/en-us/vcsharp/bb89466... Then just add the using Linq. Dynamic; and you automatically get 2 additional extension methods that can be used like this return query. OrderBy("StringColumnName").

Thanks, I had seen Linq. Dynamic about in a sample at Phil Haack's site but was unsure about it. I'll have a play with this over the weekend.

– JTew May 15 '09 at 4:49.

It seems that this is the way to do it, now to verify that: // ***** OrderBy(company => company) ***** // Create an expression tree that represents the expression // 'whereCallExpression. OrderBy(company => company)' MethodCallExpression orderByCallExpression = Expression. Call( typeof(Queryable), "OrderBy", new Type { queryableData.

ElementType, queryableData. ElementType }, whereCallExpression, Expression. Lambda>(pe, new ParameterExpression { pe })); // ***** End OrderBy.

Damnit, 34 seconds behind! :P – Slace Nov 21 '08 at 2:01.

I've extended your functions to add support for Child Properties. Private static LambdaExpression GenerateSelector(String propertyName, out Type resultType) where TEntity : class { // Create a parameter to pass into the Lambda expression (Entity => Entity. OrderByField).

Var parameter = Expression. Parameter(typeof(TEntity), "Entity"); // create the selector part, but support child properties PropertyInfo property; Expression propertyAccess; if (propertyName. Contains('.')) { // support to be sorted on child fields.

String childProperties = propertyName. Split('.'); property = typeof(TEntity). GetProperty(childProperties0); propertyAccess = Expression.

MakeMemberAccess(parameter, property); for (int I = 1; I Length; i++) { property = property.PropertyType. GetProperty(childPropertiesi); propertyAccess = Expression. MakeMemberAccess(propertyAccess, property); } } else { property = typeof(TEntity).

GetProperty(propertyName); propertyAccess = Expression. MakeMemberAccess(parameter, property); } resultType = property. PropertyType; // Create the order by expression.

Return Expression. Lambda(propertyAccess, parameter); } private static MethodCallExpression GenerateMethodCall(IQueryable source, string methodName, String fieldName) where TEntity : class { Type type = typeof(TEntity); Type selectorResultType; LambdaExpression selector = GenerateSelector(fieldName, out selectorResultType); MethodCallExpression resultExp = Expression. Call(typeof(Queryable), methodName, new Type { type, selectorResultType }, source.

Expression, Expression. Quote(selector)); return resultExp; } You can use these functions like: GenerateMethodCall(source, "OrderByDescending", fieldName).

I used your idea for extension method for OrderBy. But in case of "many to many" I am getting error. For example you have table Site, Customer and Customer_site.

For given Site I want to sort by customer name and in OrderBy extension (when I pass "site. Customer" where customer is navigation property) I get error in line: propertyAccess = Expression. MakeMemberAccess(propertyAccess, property); This is what I use (with some enhancements :-) ): public static IQueryable OrderBy(this IQueryable source, string orderByValues) where TEntity : class { IQueryable returnValue = null; string orderPair = orderByValues.Trim().

Split(',')0; string command = orderPair.ToUpper(). Contains("DESC")? "OrderByDescending" : "OrderBy"; var type = typeof(TEntity); var parameter = Expression.

Parameter(type, "p"); string propertyName = (orderPair. Split(' ')0).Trim(); System.Reflection. PropertyInfo property; MemberExpression propertyAccess; if (propertyName.

Contains('. ')) { // support to be sorted on child fields. String childProperties = propertyName.

Split('. '); property = typeof(TEntity). GetProperty(childProperties0); propertyAccess = Expression.

MakeMemberAccess(parameter, property); for (int I = 1; I (resultExpression); if (orderByValues.Trim(). Split(',').Count() > 1) { // remove first item string newSearchForWords = orderByValues.ToString(). Remove(0, orderByValues.ToString().

IndexOf(',') + 1); returnValue = source. OrderBy(newSearchForWords); } return returnValue; } Regards Slobodan.

We did something similar (not 100% the same, but similar) in a LINQ to SQL project. Here's the code.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions