0

I have two DataTables that need to be merged together. Both Tables have a field in it called "Code" Basically if one table has a certain code, it merges with the corresponding code in the other table, getting that rows values merged into it. I would say its like merging two tables together in SQL using an inner join.

Needs to be in vb.net please!

Chrisetiquette
  • 311
  • 1
  • 4
  • 22

1 Answers1

3

Here is a VB conversion of this answer.

Dim dt1 As New DataTable()
dt1.Columns.Add("CustID", GetType(Integer))
dt1.Columns.Add("ColX", GetType(Integer))
dt1.Columns.Add("ColY", GetType(Integer))

Dim dt2 As New DataTable()
dt2.Columns.Add("CustID", GetType(Integer))
dt2.Columns.Add("ColZ", GetType(Integer))

For i As Integer = 1 To 5
    Dim row As DataRow = dt1.NewRow()
    row("CustID") = i
    row("ColX") = 10 + i
    row("ColY") = 20 + i
    dt1.Rows.Add(row)

    row = dt2.NewRow()
    row("CustID") = i
    row("ColZ") = 30 + i
    dt2.Rows.Add(row)
Next

Dim results = From table1 In dt1.AsEnumerable()Join table2 In      dt2.AsEnumerable() On CInt(table1("CustID")) = CInt(table2("CustID"))New With { _
    Key .CustID = CInt(table1("CustID")), _
    Key .ColX = CInt(table1("ColX")), _
    Key .ColY = CInt(table1("ColY")), _
    Key .ColZ = CInt(table2("ColZ")) _
}
For Each item As var In results
    Console.WriteLine([String].Format("ID = {0}, ColX = {1}, ColY = {2}, ColZ = {3}", item.CustID, item.ColX, item.ColY, item.ColZ))
Next
Console.ReadLine()
Community
  • 1
  • 1
Joseph Rosson
  • 334
  • 1
  • 8
  • do you have this solution in vb.net? – Chrisetiquette Sep 08 '16 at 23:52
  • How about that? – Joseph Rosson Sep 09 '16 at 00:06
  • I'm a bit lost in your syntax, I already have all the data within two DataTables, DT1 and DT2. I am using ComboBox1.SelectedItem and ComboBox2.SelectedItem to tie the two together. If it makes it any easier because I feel like I'm not explaining myself very well, DT1 is a data list and DT2 is a matrix. I need to Code DT1 according to the matrix. So in DT1 there is a code field populated with A, B, C. I want to append the data from DT2 (the matrix) according to this code field. So, Anything in DT1 with A in the Code field will get the Row in the matrix with A in the Code field append to the end – Chrisetiquette Sep 09 '16 at 04:42
  • The syntax in LINQ. It was my understanding, based on the question, you had two datatables (in the example code above, DT1, and DT2) and wanted a resultant datatable that was the equivalent of performing an INNER JOIN on DT1 and DT2. The code does this using the LINQ "Join" operation and produces an output table called "results" that is the INNER JOIN of the two. Of course it uses a fictitious column CusID, which would be replaced by your "Code" column. – Joseph Rosson Sep 09 '16 at 14:59
  • For a LINQ example, you may find the following [link](https://msdn.microsoft.com/en-us/library/bb918093.aspx) helpful. It demonstrates, JOIN, INNER, OUTER, and LEFT equivalents to SQL, all in VB. – Joseph Rosson Sep 09 '16 at 15:02
  • I think I'm confused about the selection of specific fields, when i need it to have all the fields. And The "Key" part as well... – Chrisetiquette Sep 09 '16 at 16:03