Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify the Linq OrderBy argument dynamically? [duplicate]

Tags:

c#

linq

How do I specify the argument passed to orderby using a value I take as a parameter?

Ex:

List<Student> existingStudends = new List<Student>{ new Student {...}, new Student {...}} 

Currently implementation:

List<Student> orderbyAddress = existingStudends.OrderBy(c => c.Address).ToList(); 

Instead of c.Address, how can I take that as a parameter?

Example

 string param = "City";  List<Student> orderbyAddress = existingStudends.OrderByDescending(c => param).ToList(); 
like image 431
Sreedhar Avatar asked Sep 01 '11 01:09

Sreedhar


People also ask

What is difference between OrderBy and ThenBy in LINQ?

The point of OrderBy is to provide the "most important" ordering projection; then use ThenBy (repeatedly) to specify secondary, tertiary etc ordering projections.

What is OrderBy in LINQ?

In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.

What is Dynamic LINQ?

The Dynamic LINQ library exposes a set of extension methods on IQueryable corresponding to the standard LINQ methods at Queryable, and which accept strings in a special syntax instead of expression trees.

Does LINQ order matter?

Yes. But exactly what that performance difference is depends on how the underlying expression tree is evaluated by the LINQ provider. For instance, your query may well execute faster the second time (with the WHERE clause first) for LINQ-to-XML, but faster the first time for LINQ-to-SQL.


1 Answers

You can use a little bit of reflection to construct the expression tree as follows (this is an extension method):

public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string orderByProperty,                           bool desc)  {      string command = desc ? "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<TEntity>(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);

like image 107
Icarus Avatar answered Oct 19 '22 11:10

Icarus