Spent all day trying to find ready to use solution for “Sort data in LINQ bases on Query String” without any results.
SO, I have a LINQ query in action:
public AcrionResult MyAction(int perPage = 10, string orderBy = "DESC", sting sortBy = "id")
{
var some = from m in db.Some select new ExampleModel {id = m.id, some = m.some};
return View(some);
}
From exmaple above:
1. perPage describe how many items we should show in page
2. orderBy DESC or ASC order
3. sortBy can be some field from ViewModel
I need to make somethisng like this:
var query = some.OrderFilter(…).AmountFilter(…).SortByFilter(…)
Can somebody help me?
Thanks a lot!
You could use dynamic LINQ. Here’s another example of implementing dynamic queries.