|
-
Dec 3rd, 2009, 02:27 PM
#1
Thread Starter
Addicted Member
Cleaning up a nested for each-loop
Hi. I'm comparing a temporary table to an another table, to check for changes in the cells. The way I'm doing it, is that I'm using three loops; one to loop trough the temporary, one to loop trough the other table and a last one to loop trough each column. Here's the code:
vb.net Code:
For Each row As DataRowView In View
For Each drow As DataRowView In view2
If row("emitid") = drow("emitid") Then
For Each column As DataColumn In View.Table.Columns
If Not row(column.ToString) = drow(column.ToString) Then
row(column.ToString) = drow(column.ToString)
End If
Next
Exit For
End If
Next
Next
"emitid" is the column I use as a reference to be sure that I'm comparing the correct rows, and the column order will always be the same in both tables. Is this the best way of doing what I want? I feel it looks a bit complicated...
-
Dec 3rd, 2009, 02:37 PM
#2
Re: Cleaning up a nested for each-loop
You could probably use a recursive loop instead. I wouldn't know exactly how it would be done, but you might be able to find something via Google.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Dec 3rd, 2009, 08:28 PM
#3
Re: Cleaning up a nested for each-loop
Is 'emitid' the primary key? Will both tables have the same values in that column?
-
Dec 4th, 2009, 02:46 AM
#4
Thread Starter
Addicted Member
Re: Cleaning up a nested for each-loop
 Originally Posted by jmcilhinney
Is 'emitid' the primary key? Will both tables have the same values in that column?
Yes and yes
-
Dec 4th, 2009, 02:57 AM
#5
Re: Cleaning up a nested for each-loop
 Originally Posted by erik
Yes and yes 
In that case there's no need to loop through the second table over and over. You simply loop through the first table and then use the PK to get the corresponding row in the second table. I wouldn't worry about looping through the columns either. Just take the data from one row and put it in the other. If they are the same you haven't lost anything but if they're different you're covered.
vb.net Code:
For Each row1 As DataRow In table1.Rows
Dim row2 As DataRow = table2.Rows.Find(row1("emitid"))
row1.ItemArray = row2.ItemArray
Next
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|