Results 1 to 14 of 14

Thread: Match items from multiple DataTables

  1. #1

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Match items from multiple DataTables

    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.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Match items from multiple DataTables

    I would normally do this with a SQL Query using a In clause with a sub query

    Select Fields From table1 Where table1PK in Select table1PK from table2
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Match items from multiple DataTables

    I don't quite understand your select statement. Can you elaborate?

    Say i have two tables:

    Table1
    2 columns
    8 rows

    Table 2
    2 columns
    8 rows

    I want to find the item in column 0 from Table1 in Table2 and compare the items in column 1 from each table.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Match items from multiple DataTables

    Code:
    Select table1.column1 table2.column1 From
    table1 inner join table1.column0 = table2.column0
    or
    Code:
    Select table1.column1 table2.column1 From
    table1,table2 Where table1.column0 In Select table2.column0 From table2
    Both get the column1 value from each table where the column0 exsists in both tables.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Match items from multiple DataTables

    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.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  6. #6

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Match items from multiple DataTables

    Anyone else have an idea?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  7. #7
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Match items from multiple DataTables

    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.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  8. #8

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Match items from multiple DataTables

    I think I may have confused you.

    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.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  9. #9

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Match items from multiple DataTables

    Perhaps this bmp will help illustrate what I am trying to do....
    Attached Images Attached Images  
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  10. #10
    Junior Member
    Join Date
    Feb 2007
    Posts
    29

    Re: Match items from multiple DataTables

    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.

  11. #11

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Match items from multiple DataTables

    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.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  12. #12

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Match items from multiple DataTables

    jmcilhinney? kleinma? robdog888? I know there has to be a faster way to do it than this shotgun approach.
    Last edited by circuits2; Mar 13th, 2007 at 08:09 AM.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  13. #13

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Match items from multiple DataTables

    Bump
    Last edited by circuits2; Mar 13th, 2007 at 10:47 AM.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  14. #14

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Match items from multiple DataTables

    Bump
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

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