vb.net 在数据表中查找并获取行索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39793212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:13:32  来源:igfitidea点击:

Find and get row index in datatable

vb.netdatatable

提问by hagant

DataTable.Select problem

DataTable.Select 问题

i have simple datatable like this

我有这样的简单数据表

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

for example i want to check if 1966 is already exist in column "1" and if it exist get row index i do code like this

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

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

采纳答案by N0Alias

Unless the row index is contained in the values of the rows themselves you'll have to abandon the Select method and use a cursor based approach. This example will stop looping if a match is found:

除非行索引包含在行本身的值中,否则您将不得不放弃 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

回答by ??ssa P?ngj?rdenlarp

Using your existing loop, just find the row in the table:

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

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

Using extension methods, you can condense it:

使用扩展方法,你可以浓缩它:

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

ndxwill be an array in this case, and will be empty when there is no match.

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

回答by soohoonigan

Your query is currently trying to query number fields as strings, so if the datatable columns you're querying are indeed integers, then the correct syntax would be:

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

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

Column names and numbers don't have single quotes around them. Then, to get the index, you will need to search the table for the index of the row you returned. DataRowCollections have a way to do that, you just need to iterate through the query's return:

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

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

回答by DIPIN K

Here is the code:

这是代码:

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