且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查VB.net中DataTable中是否存在值的最简单/最快捷的方法?

更新时间:2023-11-25 20:55:40

您的 DataTable 中的数据不会更改,您会多次搜索 DataTable ,而您的 DataTable 包含很多行,那么为数据构建自己的索引可能会更快。



最简单的方法是按键列对数据进行排序,以便您可以对排序列表进行二进制搜索。例如,您可以构建如下索引:

 专用函数BuildIndex(表作为DataTable,keyColumnIndex As Integer)As List Of String)
Dim index As New List(Of String)(table.Rows.Count)
对于每一行在Table.Rows中的DataRow
index.Add(row(keyColumnIndex))
下一个
index.Sort()
返回索引
结束函数

然后,您可以使用二进制搜索来检查索引中是否存在值,如下所示:

 私人函数ItemExists(Index As List(Of String),Key As String)As Boolean 
Dim index As Integer = index.BinarySearch(key)
如果index> = 0然后
返回True
Else
返回False
结束如果
结束函数

你也可以用简单的字符串数组来做同样的事情。或者,您可以使用字典对象(这是哈希表的实现)来构建您的 DataTable $ c $的哈希索引c>,例如:

 私有函数BuildIndex(表作为DataTable,keyColumnIndex As Integer)As Dictionary(Of String,DataRow) 
Dim index As New Dictionary(Of String,DataRow)(table.Rows.Count)
For each row As DataRow in table.Rows
index(row(keyColumnIndex))= row
下一个
返回索引
结束函数

然后,你可以得到对于给定的键匹配 DataRow ,如下所示:

  Dim index As Dictionary(Of String,DataRow)= BuildIndex(myDataTable,myKeyColumnIndex)
Dim row As DataRow = Nothing
如果index.TryGetValue(myKey,row)然后找到
'行,现在可以使用行变量访问该行中的所有数据
Else
'该行不存在
如果

您可能还需要查看使用 SortedList SortedDictionary 类。这两个都是二叉树的实现。很难说这些选项中的哪一个在您的特定场景中将是最快的。这一切都取决于数据类型,索引需要重新构建的频率,搜索的频率, DataTable 中有多少行,以及您需要找到的物品。***的做法是在测试用例中尝试一个,看看哪个最适合你需要的。


I have a DataTable (currently with multiple columns but I could just grab one column if it makes it easier). I want to check if a string value exists in a column of the DataTable. (I'm doing it many times so I want it to be reasonably fast.)

What is a good way to do this? Iterating through the DataTable rows each time seems like a bad way. Can I convert the column to a flat list/array format, and use a built in function? Something like myStrList.Contains("value")?

If the data in your DataTable doesn't change very often, and you search the DataTable multiple times, and your DataTable contains many rows, then it's likely going to be a lot faster to build your own index for the data.

The simplest way to do this is to sort the data by the key column so that you can then do a binary search on the sorted list. For instance, you can build an index like this:

Private Function BuildIndex(table As DataTable, keyColumnIndex As Integer) As List(Of String)
    Dim index As New List(Of String)(table.Rows.Count)
    For Each row As DataRow in table.Rows
        index.Add(row(keyColumnIndex))
    Next
    index.Sort()
    Return index
End Function

Then, you can check if a value exists in the index quickly with a binary search, like this:

Private Function ItemExists(index As List(Of String), key As String) As Boolean
    Dim index As Integer = index.BinarySearch(key)
    If index >= 0 Then
        Return True
    Else
        Return False
    End If
End Function

You could also do the same thing with a simple string array. Or, you could use a Dictionary object (which is an implementation of a hash table) to build a hash index of your DataTable, for instance:

Private Function BuildIndex(table As DataTable, keyColumnIndex As Integer) As Dictionary(Of String, DataRow)
    Dim index As New Dictionary(Of String, DataRow)(table.Rows.Count)
    For Each row As DataRow in table.Rows
        index(row(keyColumnIndex)) = row
    Next
    Return index
End Function

Then, you can get the matching DataRow for a given key, like this:

Dim index As Dictionary(Of String, DataRow) = BuildIndex(myDataTable, myKeyColumnIndex)
Dim row As DataRow = Nothing
If index.TryGetValue(myKey, row) Then
   ' row was found, can now use row variable to access all the data in that row
Else
   ' row with that key does not exist
End If

You may also want to look into using either the SortedList or SortedDictionary class. Both of these are implementations of binary trees. It's hard to say which of all of these options is going to be fastest in your particular scenario. It all depends on the type of data, how often the index needs to be re-built, how often you search it, how many rows are in the DataTable, and what you need to do with the found items. The best thing to do would be to try each one in a test case and see which one works best for what you need.