Finding orphans among two datatables

Hi -- I've got two datatables with identical structures. I want to be able to find all of the values in one column in one of the datatables that are not in the same column in the other datatable. The values that don't match should be put into a third datatable. This is what I've got so far.

Dim dtOuter As DataTable
Dim dtInner As DataTable
Dim dtOrphans As DataTable

'Code to populate datatables omitted.

For Each row As DataRowView In dtOuter.DefaultView

Dim dv As New DataView(Table:=dtInner, _
RowFilter:="widget_name = '" & row("widget_name").ToString & "'", _
Sort:="", _
RowState:=DataViewRowState.CurrentRows)

If dv.Count = 0 Then
Dim newrow As DataRow = dtOrphans.NewRow
newrow("widget_name") = row("widget_name").ToString
dtOrphans.Rows.Add(newrow)
End If

Next

Problem is, the two datatables can have over 1,000 rows and this loop takes a long time to finish processing. Is there a more efficient way to perform this task?

[1501 byte] By [TheOtherBill] at [2007-12-24]
# 1

You don't need to create a view.

You can use the select method like:

if (dtInner.Select("widget_name=Something").Length > 0) {

// dtInner has rows whose widget_name is Something

}

Or even faster, if widget_name is primarykey column, you can do something like if (dtInner.Rows.Contains("Something"))..

BillLin-MSFT at 2007-8-31 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified