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

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

问题描述

我有一个 DataTable(目前有多个列,但如果方便的话,我可以只抓取一列).我想检查 DataTable 的列中是否存在 String 值.(我做了很多次,所以我希望它相当快.)

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.)

有什么好的方法可以做到这一点?每次都遍历 DataTable 行似乎是一种糟糕的方式.我可以将列转换为平面 List/Array 格式,并使用内置函数吗?类似于 myStrList.Contains("value")?

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")?

推荐答案

如果你的 DataTable 中的数据变化不是很频繁,并且你搜索了多个 DataTable次,并且您的 DataTable 包含许多行,那么为数据构建自己的索引可能会快得多.

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

你也可以用一个简单的字符串数组做同样的事情.或者,您可以使用 Dictionary 对象(它是哈希表的实现)来构建 DataTable 的哈希索引,例如:

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

然后,您可以获得给定键的匹配DataRow,如下所示:

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

您可能还想考虑使用 SortedListSortedDictionary 类.这两个都是二叉树的实现.很难说在您的特定场景中,所有这些选项中的哪一个是最快的.这完全取决于数据的类型,索引需要重建的频率,您搜索它的频率,DataTable 中有多少行,以及您需要对找到的数据做什么项目.最好的做法是在测试用例中尝试每一个,看看哪一个最适合您的需要.

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天全站免登陆