检查VB.net中DataTable中是否存在值的最简单/最快捷的方法? [英] Simplest/fastest way to check if value exists in DataTable in VB.net?

查看:1039
本文介绍了检查VB.net中DataTable中是否存在值的最简单/最快捷的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataTable(目前有多个列,但如果它更容易,我可以抓住一列)。我想检查一个字符串值是否存在于DataTable的一列中。 (我做了很多次,所以我想要它相当快。)



这是一个好办法?通过DataTable行迭代每次似乎都是一个糟糕的方式。可以将列转换为平面列表/数组格式,并使用内置函数吗?像 myStrList.Contains(value)

解决方案

您的 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 ,例如:

 私有函数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.

这篇关于检查VB.net中DataTable中是否存在值的最简单/最快捷的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆