sample code:
void Main()
{
var dataTable1 = GetTable1();
var dataTable2 = GetTable2();
var resultingTable = from t1 in dataTable1.AsEnumerable()
join t2 in dataTable2.AsEnumerable()
on t1.Field<int>("No") equals t2.Field<int>("No")
select new { t1, t2 };
var newDataTable =GetTable3();
foreach(var dr in resultingTable)
{
var weight1 = dr.t1.Field<int>("Weight");
var weight2 = dr.t2.Field<int>("Weight");
DataRow newRow = newDataTable.NewRow();
newRow["No"] =dr.t1.Field<int>("No");
newRow["Name"] =dr.t1.Field<string>("Name");
newRow["Weight"] = weight1;
newRow["Table1"] = weight1==weight2?"X": weight1>weight2?"X":"";
newRow["Table2"] = weight2==weight1?"X": weight2>weight1?"X":"";
newDataTable.Rows.Add(newRow);
}
}
DataTable GetTable1()
{
DataTable table = new DataTable();
table.Columns.Add("No", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Weight", typeof(int));
table.Rows.Add(1, "Ship", 500);
table.Rows.Add(2, "Train", 600);
table.Rows.Add(3, "Plane", 700);
table.Rows.Add(4, "Car", 800);
return table;
}
DataTable GetTable2()
{
DataTable table = new DataTable();
table.Columns.Add("No", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Weight", typeof(int));
table.Rows.Add(1, "Ship", 500);
table.Rows.Add(2, "Train", 400);
table.Rows.Add(3, "Plane", 800);
table.Rows.Add(4, "Car", 200);
return table;
}
DataTable GetTable3()
{
DataTable table = new DataTable();
table.Columns.Add("No", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Weight", typeof(int));
table.Columns.Add("Table1", typeof(string));
table.Columns.Add("Table2", typeof(string));
return table;
}