Using linq on OrderedDictionary to obtain max with GridView
I have a custom GridView control (inherits system.web.ui.webcontrols.gridview) and I would like to control the value of a TextBox based on values recently inserted by the user (inserted values are not committed to the datasource).
In Grid.DataBound I'm able to query the cells of a single column to get the current max which i set to a TextBox.
int maxTimeSlot = grid.Rows.OfType<GridViewRow>().Max(r => int.Parse(r.Cells[1].Text));
Aftering inserting a new row, I have access to an 'Inserts' property of the Grid which contains a list of all the inserted rows so far, for example:
GridView.Inserts[0].NewValues & GridView.Inserts[0].OldValues
What I need to do is to use this 'Inserts' list to get the max value again, but I'm struggling querying NewValues.Values (OrderedDictionaryKeyValueCollection) to get the value.
NewValues.Value开发者_高级运维s.OfType<KeyValuePair<object, object>>().Select(r => r.Key == "timeslot").Max(t => (int)t.Value)
The above is my 'idea' of how it would work, but naturally, it doesn't.
The call to Select is projecting each KeyValuePair
in NewValues.Values
to a boolean, so you no longer have access to the original data.
As it looks like you are trying to find all the "timeslot" values, I think you need something like
NewValues.Values.OfType<KeyValuePair<object, object>>().Where(r => r.Key == "timeslot").Max(t => (int)t.Value)
精彩评论