Aug 10, 2009, 10:28 AM
Using LINQ to dynamic sort data in GridPanel
Hi, everybody,
In the source code of mvc.ext.net, you can find a function names "GetCustomers" in DataController.cs. By it's signature
But reading code, it uses LINQ to implement search and sort. We kown, LINQ doesn't support order by a static string. So this line
I think below is a better method to dynamic sort data using LINQ. (by vb.net)
In the source code of mvc.ext.net, you can find a function names "GetCustomers" in DataController.cs. By it's signature
public AjaxStoreResult GetCustomers(int limit, int start, string dir, string sort)
we can know it ueses dir and sort to sort data.But reading code, it uses LINQ to implement search and sort. We kown, LINQ doesn't support order by a static string. So this line
orderby(string.Concat(sort, " ", dir))
was not working in fact.I think below is a better method to dynamic sort data using LINQ. (by vb.net)
Dim selector = GetType(Customer).GetProperty(sort)
if String.Compare("ASC", dir, true) then
query = query.OrderBy(Function(c) selector.GetValue(c, nothing))
else
query = query.OrderByDescending(Function(c) selector.GetValue(c, nothing))
end if
Maybe it needs refactor to better smell, but it's now working!