ListView, LinqDataSource, linq-to-sql and order
So I am writing a page that does some reports and it is quite dynamic, the user has the ability to group data and sort the columns. I was able to get my dynamic grouping down and then sorting, except that my generated linq-to-sql sql order by back statement is in the opposite direction from what I want. I think I need to figure out how to get the result returned from the LinqDataSource when the Sorting event is fired on the ListView so that I can then add my sentence grouping order.
I am currently overriding the LinqDataSource select event to provide its result and do dynamic grouping there, but then the result will be changed by the Sort in the list, and I thought there must be some way to get this result during the Sort or Sort event ...
Any insight will be appreciated, in the meantime I will try to figure it out myself.
Surprisingly, I think I found my answer, set AutoSort to false on my LinqDataSource, and then sort the data myself, since I'm already sorting myself for other data.
You can use Linq to manipulate the results of your queries (IEnumerables) based on custom inputs. For example (followed by a very pseudocode event handler):
public void OnUserClickNameColumn(object sender, EventArgs e){
var data = DataProvider.RetrieveAllUsers();
// switch between sorting asc and desc when user clicks on a column head
SessionHelper.CurrentUser.NameColumnSort = !SessionHelper.CurrentUser.NameColumnSort;
if(SessionHelper.CurrentUser.NameColumnSort) // true = sort asc else sort desc
UserDataGrid.DataSource = data.OrderBy(x=>x.Name);
else
UserDataGrid.DataSource = data.OrderByDescending(y=>y.Name);
}