I'm writing an asp.net mvc app and I've get a list of service calls that I display in a table. When the user clicks the table header I want to tell my controller to sort the list by that column.
public ActionResult Index(int? page, string sortBy, string sortDirection)
{
int pageIndex = page == null ? 0 : (int)page - 1;
IServiceCallService scService = new ServiceCallService();
IPagedList<ServiceCall> serviceCalls = scService.GetOpenServiceCalls("").ToPagedList(pageIndex, 2);
return View("List", serviceCalls);
}
How do I incorporate the sortBy and sortDirection. I think I could do something like:
IPagedList<ServiceCall> serviceCalls = sc.Service.GetOpenServiceCalls("").OrderBy(sortBy).ToPagedList(pageIndex, 2);
But that doesn't work because I assume OrderBy wants a lambda like p => p.CreateDate but not sure how to do this.
I know ways I could do it but they are ugly and I'm sure C# has something simple here that I'm just missing.
Thanks.