I want to find the instance of each item from one DataTable in another. There will not be duplicates.
Ordinarily I would just use For loops like this:
Code:
For Each aRow As DataRow In myDataSet.Tables("Table1").Rows
For Each bRow As DataRow In myDataSet.Tables("Table2").Rows
If bRow.Item(0) = aRow.Item(0) Then
' Found Matching Items
End If
Next bRow
Next aRow
I know that this is supremely innefficient and that there must be a better way. Can someone show me a faster method?
Show the love! Click (rate this post) under my name if I was helpful.
I understand querying tables in databases, but I didn't think SQL statements applied to DataTables within DataSets that are in memory. How would you run a SELECT statment against a table in memory?
Show the love! Click (rate this post) under my name if I was helpful.
Why not query the DB? Something preventing this? Too much time to run the query? If you can't query the datatables in memory then the only choice I know of is to walk the two datatables and check each row against each row.
The DataSet that I am working with is not in a DataBase. I have initial information that I pull from the DataBase into the DataSet, but that information is then manipulated and divided into many DataTables.
So lets start over. I have a DataSet in memory (not in a DataBase) that contains several DataTables. I want to compare the data in one DataTable to the data in another DataTable. Each item will only have 1 instance in either table.
What is the best way to do this?
Show the love! Click (rate this post) under my name if I was helpful.
how much manipulation of the data from the db did you do? I am not sure if you can query the dataset, but you can probably write sql statements that will manipulate the data from the db directly in the same fashion you manipulated the data and do the search/compare functions that you need.
I am way beyond the original information. Let's see if it is easier to attack from this angle:
Code:
' Create DataSet
Dim myDataSet As New DataSet
' Add Tables to DataSet
myDataSet.Tables.Add("Table1")
myDataSet.Tables.Add("Table2")
' Add Columns to Tables
myDataSet.Tables("Table1").Columns.Add("Size")
myDataSet.Tables("Table1").Columns.Add("Qty")
myDataSet.Tables("Table2").Columns.Add("Size")
myDataSet.Tables("Table2").Columns.Add("Qty")
'/*
' Populate Tables With Info
' .......Lots of stuff happens here
'*/
' Loop Through Each Item in Table 1
For Each aRow As DataRow In myDataSet.Tables("Table1").Rows
' Loop Through Each Item in Table 2 To Find Match
For Each bRow As DataRow In myDataSet.Tables("Table2").Rows
If bRow.Item(0) = aRow.Item(0) Then
' Found Matching Items
End If
Next bRow
Next aRow
Show the love! Click (rate this post) under my name if I was helpful.