If the data in your DataTable does not change very often, and you search for the DataTable several times, and your DataTable contains many rows, then it will most likely be much faster to create your own index for the data.
The easiest way to do this is to sort the data by the key column, then to perform a binary search on the sorted list. For example, you can create 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 the value in the index exists quickly using a binary search, for example:
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 can also do the same with a simple string array. Or you can use the Dictionary object (which is the implementation of the hash table) to create the hash index of your DataTable , for example:
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 corresponding DataRow for the given key, for example:
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 can also learn about using the SortedList or SortedDictionary . Both of them are binary tree implementations. Itβs hard to say which of these options will be the fastest in your particular scenario. It all depends on the data type, how often you need to rebuild the index, how often you look at it, how many rows are in the DataTable , and what you need to do with the items found. It would be best to try each one in a test case and see which one is best for what you need.