【问题标题】:Using linq on OrderedDictionary to obtain max with GridView在 OrderedDictionary 上使用 linq 通过 GridView 获取最大值
【发布时间】:2011-06-21 15:53:30
【问题描述】:

我有一个自定义 GridView 控件(继承 system.web.ui.webcontrols.gridview),我想根据用户最近插入的值(插入的值)控制 TextBox 的值未提交到数据源)。

在 Grid.DataBound 中,我可以查询单个列的单元格以获取我设置为 TextBox 的当前最大值。

int maxTimeSlot = grid.Rows.OfType<GridViewRow>().Max(r => int.Parse(r.Cells[1].Text));

插入新行后,我可以访问 Grid 的“Inserts”属性,其中包含到目前为止所有插入行的列表,例如:

GridView.Inserts[0].NewValues & GridView.Inserts[0].OldValues

我需要做的是使用此“插入”列表再次获取最大值,但我正在努力查询 NewValues.Values (OrderedDictionaryKeyValueCollection) 以获取该值。

NewValues.Values.OfType<KeyValuePair<object, object>>().Select(r => r.Key == "timeslot").Max(t => (int)t.Value)

以上是我对它如何工作的“想法”,但自然不会。

【问题讨论】:

  • NewValues 的类型是什么?
  • GridViewUpdateEventArgs.NewValues = OrderedDictionary.... NewValues.Values = OrderedDictionary.OrderedDictionaryKeyValueCollection

标签: c# linq max ordereddictionary


【解决方案1】:

对 Select 的调用将 NewValues.Values 中的每个 KeyValuePair 投影为布尔值,因此您不再有权访问原始数据。

看起来您正在尝试查找所有“时间段”值,我认为您需要类似的东西

NewValues.Values.OfType<KeyValuePair<object, object>>().Where(r => r.Key == "timeslot").Max(t => (int)t.Value)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2017-10-25
    • 2012-12-15
    • 1970-01-01
    相关资源
    最近更新 更多