I am trying to sort data which is from Generic List object.
By using below code, I can sort my data according to Title column.
But what I would like to do is I would like to sort data according to parameter value called sidx.
public ActionResult ListingGridData(string sidx, string sord, int page, int rows)
{
int currentPage = Convert.ToInt32(page) - 1;
int totalRecords = 0;
var SeminarList = (List<Seminar>)null;
if(sord.Equals("asc"))
SeminarList = seminarRepository.AllSeminarList().Seminar_Masters
.OrderBy(x => x.Title )
.Skip(currentPage * rows)
.Take(rows)
.ToList();
else
SeminarList = seminarRepository.AllSeminarList().Seminar_Masters
.OrderByDescending(x => x.Title)
.Skip(currentPage * rows)
.Take(rows)
.ToList();
totalRecords = seminarRepository.AllSeminarList().Seminar_Masters.Count;
var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
....
}
So I have modified my code like that, but it does not work.
It does not sort as I have expected.
SeminarList = seminarRepository.AllSeminarList().Seminar_Masters
.OrderBy(x => sidx)
.Skip(currentPage * rows)
.Take(rows)
.ToList();
The object which I now use is pure List object, not IQueryable object.
As for sorting purpose, I don’t want to go back to SQL select statement, I would like to sort data at Controller layer.
Please let me get any suggestions.
See answer for dynamic where and orderby using some helper
methods here
where can I find a good example of using linq & lambda expressions to generate dynamic where and orderby sql?