แล้วจับ datatable ทั้งสองนั้นมา join กันทีหลัง โดยใช้ class นี้
JoinDataTable.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
/// <summary>
/// Summary description for SqlDatabase
/// </summary>
public class JoinDataTable
{
public JoinDataTable()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// Join DataTable with DataTable.
/// </summary>
public static DataTable Join(DataTable LeftTable, DataTable RightTable, string LeftPrimaryColumn, string RightPrimaryColumn)
{
//first create the datatable columns
DataSet mydataSet = new DataSet();
mydataSet.Tables.Add(" ");
DataTable myDataTable = mydataSet.Tables[0];
//add left table columns
DataColumn[] dcLeftTableColumns = new DataColumn[LeftTable.Columns.Count];
LeftTable.Columns.CopyTo(dcLeftTableColumns, 0);
foreach (DataColumn LeftTableColumn in dcLeftTableColumns)
{
if (!myDataTable.Columns.Contains(LeftTableColumn.ToString()))
myDataTable.Columns.Add(LeftTableColumn.ToString());
}
//now add right table columns
DataColumn[] dcRightTableColumns = new DataColumn[RightTable.Columns.Count];
RightTable.Columns.CopyTo(dcRightTableColumns, 0);
foreach (DataColumn RightTableColumn in dcRightTableColumns)
{
if (!myDataTable.Columns.Contains(RightTableColumn.ToString()))
{
if (RightTableColumn.ToString() != RightPrimaryColumn)
myDataTable.Columns.Add(RightTableColumn.ToString());
}
}
//add left-table data to mytable
foreach (DataRow LeftTableDataRows in LeftTable.Rows)
{
myDataTable.ImportRow(LeftTableDataRows);
}
ArrayList var = new ArrayList(); //this variable holds the id's which have joined
ArrayList LeftTableIDs = new ArrayList();
LeftTableIDs = DataSetToArrayList(0, LeftTable);
//import righttable which having not equal Id's with lefttable
foreach (DataRow rightTableDataRows in RightTable.Rows)
{
if (LeftTableIDs.Contains(rightTableDataRows[0]))
{
string wherecondition = "[" + myDataTable.Columns[0].ColumnName + "]='"
+ rightTableDataRows[0].ToString() + "'";
DataRow[] dr = myDataTable.Select(wherecondition);
int iIndex = myDataTable.Rows.IndexOf(dr[0]);
foreach (DataColumn dc in RightTable.Columns)
{
if (dc.Ordinal != 0)
myDataTable.Rows[iIndex][dc.ColumnName.ToString().Trim()] =
rightTableDataRows[dc.ColumnName.ToString().Trim()].ToString();
}
}
else
{
int count = myDataTable.Rows.Count;
DataRow row = myDataTable.NewRow();
row[0] = rightTableDataRows[0].ToString();
myDataTable.Rows.Add(row);
foreach (DataColumn dc in RightTable.Columns)
{
if (dc.Ordinal != 0)
myDataTable.Rows[count][dc.ColumnName.ToString().Trim()] =
rightTableDataRows[dc.ColumnName.ToString().Trim()].ToString();
}
}
}
return myDataTable;
}
private static ArrayList DataSetToArrayList(int ColumnIndex, DataTable dataTable)
{
ArrayList output = new ArrayList();
foreach (DataRow row in dataTable.Rows)
output.Add(row[ColumnIndex]);
return output;
}
}