Comparing DataTables and Removing Rows VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AllBeagle
    New Member
    • Sep 2008
    • 13

    Comparing DataTables and Removing Rows VB.NET

    Hey Everyone,

    I'm having an issue that I hope someone can help me out with. If I have 2 DataTables (one that reads data from a DB and one that reads from a XML file), is there a way to combine them and eliminate any records that are duplicated. What I'm trying to do is build an app for a realtor that uses a RETS Connector, and I'm required to write the data to a XML file rather than another database for reasons that really aren't worth going into. But if you aren't familiar with a RETS Connection, you have to pull the data, write it how you want, and then you can use it on your site to your liking. They limit the connections so I can't just feed off of their database.

    The client wants to be able to update the properties on a daily basis, so in order to eliminate a 12-hour wait time to download the property data by starting over, I'm trying to use the current xml file to fill a data table and compare it to another data table that is created when I search the RETS feed. If I can combine the data tables after they are filled and possibly combine/sort them, then eliminate any records that are duplicated (remove the original and duplicate), that will leave me with only the new properties. Then I can take that data to do the actual request for images, etc. Did I lose you?

    If anyone has any ideas or links, please send them my way. Many thanks for any help.

    This is a VB.NET web application.
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Hmm, quick hack perhaps, create a third datatable that is a copy of your first, and then iterate through the second table and where each row doesn't exist, add it to the table. Now table 3 holds all the unique records...

    LINQ would be a great tool for this.

    Comment

    • AllBeagle
      New Member
      • Sep 2008
      • 13

      #3
      Originally posted by balabaster
      Hmm, quick hack perhaps, create a third datatable that is a copy of your first, and then iterate through the second table and where each row doesn't exist, add it to the table. Now table 3 holds all the unique records...

      LINQ would be a great tool for this.

      Thanks for the quick reply! Do you know what the syntax is for determining if a row exists. Sorry I'm still kind of new at this stuff. I had a table.select(fi lterexpression) set up before and used a loop to go through and try to find records but it took forever. I'll look into LINQ, but I'm on a time limit for this, so if it's something that's going to require me to do a lot of research and code changes to implement, I think it's off the table...no pun intended :).

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by AllBeagle
        Thanks for the quick reply! Do you know what the syntax is for determining if a row exists. Sorry I'm still kind of new at this stuff. I had a table.select(fi lterexpression) set up before and used a loop to go through and try to find records but it took forever. I'll look into LINQ, but I'm on a time limit for this, so if it's something that's going to require me to do a lot of research and code changes to implement, I think it's off the table...no pun intended :).
        um...not exactly sure as I've not yet had to use LINQ for an "Exists" clause...I just hacked this together, so it'll need some tweaking, but it should give you an idea:
        Code:
                Dim tbl1 As New DataTable 'The table from the database
                Dim tbl2 As New DataTable 'The table from the XML (the ones we previously downloaded I'm assuming)
                Dim qry = From x In tbl1 _
                          Where Not (From y In tbl2 _
                                     Select y.Field("KeyColumn")).Contains(x.Field("KeyColumn")) _
                          Select x
        
                For Each TableRow In qry
                    'Add the content of the row to the XML
                Next

        Comment

        Working...