I am trying to achieve a dynamic way of ordering my database results by a string parameter passed into my controller sortByColumn
but I cannot see how I would change OrderBy
to allow for this. I guess I would need some kind of evaluation function? Is there a better way?
public ViewResult List(int page = 1, string? sortByColumn = null)
{
JamesListViewModel model = new JamesListViewModel
{
James = repository.James
//.OrderBy(s => s.Name)
.OrderBy(s => s.sortByColumn) // This line is junk :(
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = repository.James.Count()
}
};
return View(model);
}