Reputation: 41832
In my project there are two datatables dtNames
and dtDetails
.
I dont know about SQL. Here I am connecting to XML and Textfiles.
dtNames
EmpName EmpRole
a 2
b 3
c 4
dtDetails
Empid EmpName EmpCity EmpRole
101 a abc 3 //EmpRole not equal to above table EmpRole
102 b abc 3
103 c xyz 4
104 d pqr 5
105 e rst 6
I want to relate these two datatables based on the EmpName
and EmpRole
(here comparing to dtNames
) and store the result in a DataSet dsMain
(table from dtDetails
) and then divide the two datatables in according to comparison like matchedDataTable
and unmatchedDataTable
.
I know this can be done using DataRelation or RowFilter but i cant get a thought how to do this because there are two columns to be compared with other datatable two columns which i dont know.(I am beginner in .net)
I tried the below code: (not working)
DataSet dsMain = new DataSet();
DataRelation newRelation = new DataRelation("processData"
, dtNames.Columns["EmpName"], dtDetails.Columns["EmpName"]);
dsMain.Relations.Add(newRelation);
As you can see In the above code I am just comparing one column with other column. How compare with two. I am very close
please assist.
Upvotes: 3
Views: 21987
Reputation: 2171
Use the constructor that accepts column arrays:
DataSet dsMain = new DataSet();
DataRelation newRelation = new DataRelation("processData",
new DataColumn[] { dtNames.Columns["EmpName"], dtNames.Columns["EmpRole"] },
new DataColumn[] {dtDetails.Columns["EmpName"], dtDetails.Columns["EmpRole"]}
);
dsMain.Relations.Add(newRelation);
Upvotes: 7
Reputation: 911
Did you check the possible constructors of DataRelation
?
Just pass an array of DataColumn
, and you can combine multiple columns.
Like so:
DataRelation newRelation = new DataRelation("test", new DataColumn[] { dt1.Columns[0], dt1.Columns[1] },
new DataColumn[] { dt2.Columns[0], dt2.Columns[1] });
Upvotes: 4