【问题标题】:Find and get row index in datatable在数据表中查找并获取行索引
【发布时间】:2017-02-09 03:09:49
【问题描述】:

DataTable.Select 问题

我有这样的简单数据表

|   1  |  2   |   3  |
|------|------|------|
| 1966 | 6544 | 1967 | 
| 9560 | 3339 | 4968 | 
| 0    | 9400 | 1765 | 
| 0    | 5479 | 6701 | 

例如,我想检查“1”列中是否已经存在 1966,如果存在则获取行索引 我做这样的代码

Dim search() As DataRow = table.Select(" '" & i & "' = '" & value & "'   ")
  'where i is a integer from 1 to 3 and value is a biginteger
    If search.Count > 0 Then
        'get row index
    Else
        Console.WriteLine("not found")
    End If

【问题讨论】:

    标签: vb.net datatable


    【解决方案1】:

    使用您现有的循环,只需在表格中找到该行:

    Dim ndx As Int32
    Dim rows = dtSample.Select("Id = 42")
    If rows.Count > 0 Then
        ndx = dtSample.Rows.IndexOf(rows(0))
    End If
    Return ndx
    

    使用扩展方法,可以将其压缩:

    Dim ndx = dtSample.AsEnumerable().
                    Where(Function(q) q.Field(Of Int32)("Id") = 42).
                    Select(Function(z) dtSample.Rows.IndexOf(z)).
                    ToArray()
    

    ndx在这种情况下是一个数组,没有匹配的时候为空。

    【讨论】:

    • IndexOf 的好用处。
    • 我仍然无法使用 table.Select Dim rows = table.Select("1 = 1966") 并使用这样的条件 'If rows.Count > 0 Then @Plutonix
    【解决方案2】:

    您的查询当前正在尝试将数字字段作为字符串进行查询,因此如果您查询的数据表列确实是整数,那么正确的语法应该是:

    Dim search() As DataRow = table.Select(i.ToString & " = " & value.ToString)
    

    列名和数字周围没有单引号。然后,要获取索引,您需要在表中搜索您返回的行的索引。 DataRowCollections 有办法做到这一点,您只需要遍历查询的返回:

    For Each dr As DataRow In table.Select(i.ToString & " = " & value.ToString)
        MsgBox(dr.Table.Rows.IndexOf(dr).ToString)
    Next dr
    

    【讨论】:

      【解决方案3】:

      代码如下:

      Dim rowIndex = dt.AsEnumerable().[Select](Function(r) r.Field(Of String)("Column_Name")).ToList().FindIndex(Function(col) col = "Find_Key")
      

      【讨论】:

      • 请遵循how to answer 指南。您的回答遗漏了一些说明。
      • 这对我很有用。正是我需要的,谢谢!我确实注意到如果索引不存在,它将返回索引为-1。因此,您只需执行“如果”语句,说明如果索引大于 0,则...无论您需要什么。
      【解决方案4】:

      除非行索引包含在行本身的值中,否则您将不得不放弃 Select 方法并使用基于游标的方法。如果找到匹配项,此示例将停止循环:

      Dim intTargetIndex As Integer = -1
      Dim intCursor As Integer = 0
      
      Do Until intCursor = table.Rows.Count OrElse intTargetIndex > -1
      
          If table.Rows(intCursor)(0).ToString() = value.ToString() Then
      
              intTargetIndex = intCursor
      
          End If
      
          intCursor += 1
      
      Loop
      

      【讨论】:

      • 您的代码可以很好地解决我的问题,但它是否比使用 table.Select 更好?.. @NoAlias
      • 我猜这会比 table.Select 方法快一点。首先,它会在找到一个匹配项时停止(类似于 .Any() vs .Count() > 0),其次 .IndexOf 可能更昂贵。最好的确定方法是分析这两种方法。
      【解决方案5】:

      首先,选择您的数据表,然后使用For Each 获取行索引。

      Dim result() As DataRow = tblchk.Select("Device_No ='" & TxtBarcode.Text & "'")
      For Each row As DataRow In result
          MsgBox(row.Table.Rows.IndexOf(row)) ''this is for row index value
      Next
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多