Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I order by multiple fields when using Linq.Dynamic?

Tags:

.net

linq

In standard linq when I use lambdas I can do the following:

var x = _usersService.GetAll().OrderBy(u => u.LastName).ThenBy(u => y.FirstName).ThenBy(u => u.UserId)

My question is how can I do that when I'm using System.Linq.Dynamic dll? I know I can do:

var x = _usersService.GetAll().OrderBy("LastName")

but how can I define additional order by clauses???

like image 487
Marko Avatar asked Sep 12 '14 19:09

Marko


People also ask

Can you use Linq on dynamic?

It's possible to build up dynamic LINQ queries or queries with several conditional criteria. In fact there are several options for doing this, including the use of expression trees.

What is Linq dynamic?

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.


1 Answers

You can use comma.

var x = _usersService.GetAll().OrderBy("LastName,FirstName,UserId")

You can also add desc or descending to order by descending.

var x = _usersService.GetAll().OrderBy("LastName desc,FirstName desc,UserId")
like image 85
Yuliam Chandra Avatar answered Oct 07 '22 01:10

Yuliam Chandra