pi...." />

How do I specify the Linq OrderBy argument dynamically?

Here's a possiblity using reflection var param = "Address"; var pi = typeof(Student). GetProperty(param); var orderByAddress = items. OrderBy(x => pi.

GetValue(x, null)).

Here's a possiblity using reflection... var param = "Address"; var pi = typeof(Student). GetProperty(param); var orderByAddress = items. OrderBy(x => pi.

GetValue(x, null)).

This is what I was looking for. Was missing the . GetValue portion from my original answer.

– tsells Sep 1 at 2:17.

This doesn't let you pass a string, as you asked for in your question, but it might still work for you. The OrderByDescending method takes a Func, so you can rewrite your function this way: List QueryStudents(Func orderBy) { return existingStudents. OrderByDescending(orderBy).ToList(); } There are other overloads for OrderByDescending as well that take a Expression, and/or a IComparer.

You could also look into those and see if they provide you anything of use.

There is an example of how to do this in the Samples installed with Visual Studio. C:\Program Files (x86)\Microsoft Visual Studio 10.0\Samples\1033 Look for the Linq\Dynamic Linq solution.

You're returning a PropertyInfo, not the value of the property. Admittedly that's only one step farther, but I'm still not sure that will work either, since you'll get back object – Merlyn Morgan-Graham Sep 1 at 1:25 This doesn't work. – BrokenGlass Sep 1 at 1:27 This will not work.

This is same as what am doing in my second statement List orderbyAddress = existingStudends. OrderByDescending(c => param).ToList(); – Nev_Rahd Sep 1 at 1:29 Yeah I see that - thinking further on this...I meant to do p. PropertyType but I think another thing needs to be added... – tsells Sep 1 at 1:32.

"OrderByDescending" : "OrderBy"; var type = typeof(TEntity); var property = type. GetProperty(orderByProperty); var parameter = Expression. Parameter(type, "p"); var propertyAccess = Expression.

MakeMemberAccess(parameter, property); var orderByExpression = Expression. Lambda(propertyAccess, parameter); var resultExpression = Expression. Call(typeof(Queryable), command, new Type { type, property.

PropertyType }, source. Expression, Expression. Quote(orderByExpression)); return source.Provider.

CreateQuery(resultExpression); } orderByProperty is the Property name you want to order by and if pass true as parameter for desc, will sort in descending order; otherwise, will sort in ascending order. Now you should be able to do existingStudents. OrderBy("City",true); or existingStudents.

OrderBy("City",false).

Var orderByExpression = Expression. Var resultExpression = Expression. Call(typeof(Queryable), command, new Type { type, property.

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