Search code examples
vb.netdatatablerowfilter

Filter specific row from one data table to other data table


I am using Windows Application. I have two data tables called "dt1" and "dt2".

In "dt1" i have some rows like

ID Name Group
1  A1    G1
2  A2    G1
2  A2    G2
3  A3    G2
3  A3    G1
4  A4    G2
5  A5    G2 

AND In second table "dt2",

ID NAME  
1  A1
2  A2
3  A3
4  A4
5  A5
6  A6

Now i want rows of "dt2" from filtered row "dt1" say id - 1,2 which is also present in "dt2".

So basically i want rows for one data table from other data table which is filtered.

Can any one give some idea how to get this?


Solution

  • The most efficient and easiest would be to use Linq-To-DataSet and join both tables:

    Dim result = From r1 In dt1.AsEnumerable()
                 Join r2 In dt2.AsEnumerable()
                 On r1.Field(Of Int32)("ID") Equals r2.Field(Of Int32)("ID")
                 Select r2
    

    If you want the result, what is an IEnumerable(Of DataRow), as DataTable:

    Dim tblResult = result.CopyToDataTable()