I have to implement sorting. While i want to do this by not hard coding the model like x.SlNo
below.
public ActionResult Index(string sortOrder, string sortDirection, int? itemsPerPage, int? page, string SearchString)
{
IPagedList<TransferBenefits> entityList = null;
sortOrder = String.IsNullOrEmpty(sortOrder) ? "SlNo" : sortOrder;
var propertyInfo = typeof(TransferBenefits).GetProperty(sortOrder);
var selectList = db.transferBenefits.OrderBy(x => propertyInfo.GetValue(x, null));
//selectList = db.transferBenefits.OrderBy(x => x.SlNo); --uncommenting this works.
entityList = selectList.ToPagedList(pageNumber: page ?? 1, pageSize: itemsPerPage ?? 10);
return View(entityList)
}
I am getting this error while uncommenting code runs fine.
An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object, System.Object[])'
method, and this method cannot be translated into a store expression.