I have 2 DataTables in vb.net. Each is populated from it's own stored procedure. Table A contains a project number in the first column. Table B also contains the project number in the first column. Table A could have many records that have the same project number, but Table B will always have just one record in it per project number. I would like to append the data from Table B to every matching record in Table A. How would I do this?
Table A could look like this:
PROJECT#, QUANTITY
12345, 100
12345, 200
12345, 300
Table B could look like this:
PROJECT#, CUSTOMER
12345, ABC Inc.
I would like to merge the two to create something like this:
PROJECT#, QUANTITY, CUSTOMER
12345, 100, ABC Inc.
12345, 200, ABC Inc.
12345, 300, ABC Inc.
Please help!
This may help you, and may be semi-generic enough to be applied to other situations.
It's a function that will merge the data (as per your example) by passing in the two tables, two arrays containing the column names you require from each table, and the key used to join the tables.
There is an assumption that tblA is the driving table, with a lookup into tblB.
I'd advise using the LINQ methods, LINQ has a join operator that can handle this.
anything AsEnumerable can be used in LINQ
var quantities = DataTable1().AsEnumerable(); var customers = DataTable2().AsEnumerable();
Its further explanation of Chris Chilvers
From q In quantities _ Join c In customers On q.project Equals c.project _ Select Quantity = q, Customer = c