Why won't this sequence of entities sort in the GridView after binding?
This sequence I created works well in LINQPad
IQueryable<AccessRequest> filteredRows1;
// retrieve the last record of each request in its request history
开发者_开发知识库filteredRows1 = from req in AccessRequestSet
group req by req.TaskReferenceId
into g
let topReq = g.OrderByDescending(r => r.UpdateDate).FirstOrDefault()
select topReq;
IQueryable<AccessRequest> filteredRows =
filteredRows1.Where(o => o.Status == "Requested");
I want to then sort these entities by the field, RequestedReleaseTime.
filteredRows.OrderBy(p => p.RequestedReleaseTime);
Again I'm grouping by one DateTime successfully, and then sorting by another DateTime successfully, confirmed in LINQPad. I then add this as a DataSource and bind to my GridView:
gvRequests.DataSourceID = string.Empty;
gvRequests.DataSource = filteredRows;
gvRequests.DataBind();
I have even set AllowSorting="true" and added this line to Sort.
gvRequests.Sort("RequestedReleaseTime", SortDirection.Ascending);
I have tried this last line before and after the DataBind to no avail. What is wrong with this approach? Should I be doing any of this within the OnSorted or OnSorting events? Note: I tried that and got StackOverflowException, which reminded me that I should be asking you guys. Thanks millions for any direction or help I receive.
Calling OrderBy
doesn't actually modify the original query, but instead returns a new query based on the original, which you need to pick up:
var filteredRowsOrdered = filteredRows.OrderBy(p => p.RequestedReleaseTime);
I believe the gridview's "sort" is intended to work with something like an ObjectDataSource, which knows to call a method to retrieve the data and pass a sort string into that method. Unfortunately, this doesn't work very well with Entity Framework. We've figured out some tricks to make our data-access classes figure out how to convert a sort expression into a q = q.OrderBy(...)
statement. But it can be complicated.
精彩评论