I am using datatables in quite a few pages in my ASP.NET MVC 3 site. They use server side paging and now I want to implement sorting based on the column headers. Datatables comes with iSortCol_0
which is an int value of what column is clicked.
I did not like this approach as the query would end up something like:
if(iSortCol_0 == 0)
{
// query
// OderBy(x => x.CarName)
}
Then this would be repeated for each column (plus an else clause on each to order by descending). So I have changed my approach and now pass the column name to the server.
I have come up with the following:
Expression<Func<vw_Car, string>> sortExpression1 = null;
Expression<Func<vw_Car, int>> sortExpression2 = null;
switch(columnToSort)
{
case "InvoiceNo": sortExpression1 = x => x.CarNo; break;
case "ClientNumber": sortExpression1 = x => x.ForeName; break;
case "ClientName": sortExpression1 = x => x.SurName; break;
default: sortExpression2 = x => x.Age.Value; break;
}
// start of query
.OrderByDir(sortDirection, sortExpression1 , sortExpression2)
Now the OrderByDir
looks like below:
public static IOrderedQueryable<T> OrderByDir<T>(this IQueryable<T> source, string dir, Expression<Func<T, string>> column1, Expression<Func<T, int>> column2)
{
if (column1 != null)
{
return dir == "asc" ? source.OrderBy(column1) : source.OrderByDescending(column1);
}
if (column2 != null)
{
return dir == "asc" ? source.OrderBy(column2) : source.OrderByDescending(column2);
}
return null;
}
This works in that it sorts columns that are of type string
or int
. I have another column of DateTime
that I want to sort, so I would need to write another sortExpression3
and then add that to my OrderByDir
.
However I don't really like the implementation - I had attempted to write a generic sort expression that took an object as second parameter instead of string
, int
, Datetime
, but when had this code in place, I was getting Unable to cast the type 'System.DateTime' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.
Any one any ideas as to a possible better approach to this?