Linq Sort Direction From String

I am trying to sort a set of users. I have access to the sort property and direction (asc, desc). My current order is on request below. But, as you can see, it does not take into account the direction of sorting. How can I build this expression without using Dynamic Linq or adding another set of instructions for the direction of sorting by "asc" or "desc".

public override IQueryable<DalLinq.User> GetSort(IQueryable<DalLinq.User> query) { //SelectArgs.SortDirection <- Sort Direction switch (SelectArgs.SortProperty) { case "LastName": query = query.OrderBy(p => p.LastName); break; case "FirstName": query = query.OrderBy(p => p.FirstName); break; default: query = query.OrderBy(p => p.UserName); break; } return query; } 
+6
sorting c # linq linq-to-sql sortdirection
source share
3 answers

Ideally, you want to use OrderByDescending - you could, of course, cheat:

 public static class MyExtensionMethods { public static IOrderedQueryable<TSource> OrderBy<TSource,TValue>( this IQueryable<TSource> source, Expression<Func<TSource,TValue>> selector, bool asc) { return asc ? source.OrderBy(selector) : source.OrderByDescending(selector); } } 

And use OrderBy passing in selector and bool?

If you do not need static typing, you can also dynamically create expressions from scratch, for example, this short sample (similar in nature to the LINQ dynamic library).

+12
source share

This would be an if statement, I think, of another simple way to do this, i.e.:

 query = (SelectArgs.SortDirection == "asc") ? query.OrderBy(p => p.LastName) : query.OrderByDescending(p => p.LastName); 

See also: Sorting a List Using Lambda / Linq for Objects

+2
source share

Take a look at the CS code samples. There are dynamic examples of Linq.

From samples:

 Northwind db = new Northwind(connString); db.Log = Console.Out; var query = db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10). OrderBy("CompanyName"). Select("New(CompanyName as Name, Phone)"); 

Order by code:

  public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, params object[] values) { return (IQueryable<T>)OrderBy((IQueryable)source, ordering, values); } public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values) { if (source == null) throw new ArgumentNullException("source"); if (ordering == null) throw new ArgumentNullException("ordering"); ParameterExpression[] parameters = new ParameterExpression[] { Expression.Parameter(source.ElementType, "") }; ExpressionParser parser = new ExpressionParser(parameters, ordering, values); IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering(); Expression queryExpr = source.Expression; string methodAsc = "OrderBy"; string methodDesc = "OrderByDescending"; foreach (DynamicOrdering o in orderings) { queryExpr = Expression.Call( typeof(Queryable), o.Ascending ? methodAsc : methodDesc, new Type[] { source.ElementType, o.Selector.Type }, queryExpr, Expression.Quote(Expression.Lambda(o.Selector, parameters))); methodAsc = "ThenBy"; methodDesc = "ThenByDescending"; } return source.Provider.CreateQuery(queryExpr); } 

But make sure you check User Input!

0
source share

All Articles