I have a collection with name,date,category as parameters. I want to sort the list based on the input order of the three which changes dynamically. For ex: in one case i have to sort on the order date,name, category respectively. in another case the order is name, date and category.So i can have all the permutation and combinations. Can someone help me with a generic way to implement it.
Asked
Active
Viewed 1,352 times
0
-
possible duplicate of [Multiple "order by" in LINQ](http://stackoverflow.com/questions/298725/multiple-order-by-in-linq) – Panagiotis Kanavos Sep 24 '13 at 06:46
-
1http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet – JohnLBevan Sep 24 '13 at 06:48
-
LINQ provides this out of the box using the ThenBy function, no need to use Dynamic Linq or anything – Panagiotis Kanavos Sep 24 '13 at 06:50
-
i know that LinQ has orderby and thenby function. But the problem here is since the order of fields is dynamic, how do i pass the field and arrange them accordingly ? – user1687824 Sep 24 '13 at 07:06
3 Answers
1
LINQ already allows you to sort using multiple fields by adding the Queryable.ThenBy, QueryableThenByDescending, Enumerable.ThenBy or Enumerable.ThenByDescending functions after OrderBy.
Check Multiple “order by” in LINQ for a similar question and the discussion.
As an aside, you can convert an Enumerable to a Queryable simply calling AsQueryable on it, if you need a rare method that is not provided by Enumerable.

Community
- 1
- 1

Panagiotis Kanavos
- 120,703
- 13
- 188
- 236
-
i know that LinQ has orderby and thenby function. But the problem here is since the order of fields is dynamic, how do i pass the field and arrange them accordingly ? – user1687824 Sep 24 '13 at 07:21
-
You don't need to *pass* anything, just call ThenBy with the appropriate lambda depending on the field you want to order by. Even a simple switch statement is OK. Or, you can create an array/list with the lambdas you want to order by and apply them one by one. A lambda is just another type of variable after all – Panagiotis Kanavos Sep 24 '13 at 07:25
-
i am not sure if you ve understood me rightly or i did not get it ...can you gimme the sample code ? According to yoursolution for the first field i need to give orderby. and for the rest its thenby? so how do make it possible dynamically ? – user1687824 Sep 24 '13 at 08:40
0
I also found here something that he might like: http://www.codeproject.com/Articles/27834/Generic-List-Sort-Function <-- hope it helps, it sorts by any input property. The property is taken through reflection.

Andrei Neculai
- 472
- 2
- 5
- 21
-
the method is implemented in List and not IQueryable. So can only be used after a .ToList() or in other words after the data has been loaded. – Parv Sharma Sep 24 '13 at 06:48
0
Here is an example
// Just to get IOrderedEnumerable from the collection
// And set the default order
var orderedItems = context.Items.OrderBy(i => i.Name);
var orderByString = txtOrderBy.Text.ToLower();
switch(orderByString)
{
case "category":
orderedItems = orderedItems.OrderBy(i => i.Category);
break;
case "date":
orderedItems = orderedItems.OrderBy(i => i.Date);
break;
default:
break;
}

Ahmed Magdy
- 5,956
- 8
- 43
- 75
-
1This does NOT order by a set of fields, only the last OrderBy field counts. – Panagiotis Kanavos Sep 24 '13 at 06:56