【问题标题】:Get the row next to the row got with DataTable.Select()获取使用 DataTable.Select() 获取的行旁边的行
【发布时间】:2012-04-24 16:35:18
【问题描述】:

我有下一个数据的 DataTable 对象:

Id Value
1   val1
3   val2
2   val3

我正在选择下一行:

 table.Select("Id=1");

这个查询给了我一行

 1 val1

接下来我要取所选行旁边的行,即行

 3 val2

我能以某种方式做到这一点吗?可能吗? 也许有类似行在表格中的位置的东西,我可以通过增加这个值来选择下一行?

【问题讨论】:

    标签: c# datatable datarow


    【解决方案1】:

    DataTable.Rows.IndexOf() 是我唯一能想到的。

    如何:

    var rows=table.Select("Id=1");
    var indexOfRow=table.Rows.IndexOf(rows[0]); //since Id only 1 match
    
    var nextRow=table.Rows[indexOfRow+1];
    

    例子:

        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof (int));
        dt.Columns.Add("AnotherID", typeof(int));
        dt.Columns.Add("Content", typeof(string));
        dt.PrimaryKey = new[] {dt.Columns["ID"]};
    
        // Add some data
        dt.Rows.Add(1, 10, "1");
        dt.Rows.Add(2, 11, "2");
        dt.Rows.Add(3, 12, "3");
        dt.Rows.Add(4, 13, "4");
    
        var index = dt.Rows.IndexOf(dt.Rows.Find(3));
    
        // index is 2
    
        Console.WriteLine(dt.Rows[index+1]);
    

    在 Linqpad 中输出

    数据行
    编号 4
    另一个ID 13
    内容4

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-13
      • 2019-03-14
      • 2011-09-08
      • 1970-01-01
      • 2014-07-17
      • 2022-06-14
      • 2013-01-13
      • 2017-06-03
      相关资源
      最近更新 更多