Results 1 to 5 of 5

Thread: Cleaning up a nested for each-loop

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    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:
    1. For Each row As DataRowView In View
    2.                     For Each drow As DataRowView In view2
    3.                         If row("emitid") = drow("emitid") Then
    4.                             For Each column As DataColumn In View.Table.Columns
    5.                                 If Not row(column.ToString) = drow(column.ToString) Then
    6.                                     row(column.ToString) = drow(column.ToString)
    7.                                 End If
    8.                             Next
    9.                             Exit For
    10.                         End If
    11.                     Next
    12.                 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...

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Cleaning up a nested for each-loop

    Is 'emitid' the primary key? Will both tables have the same values in that column?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    Re: Cleaning up a nested for each-loop

    Quote Originally Posted by jmcilhinney View Post
    Is 'emitid' the primary key? Will both tables have the same values in that column?
    Yes and yes

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Cleaning up a nested for each-loop

    Quote Originally Posted by erik View Post
    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:
    1. For Each row1 As DataRow In table1.Rows
    2.     Dim row2 As DataRow = table2.Rows.Find(row1("emitid"))
    3.  
    4.     row1.ItemArray = row2.ItemArray
    5. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width