using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication2
{
public partial class TwoDataTableJoin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("id");
dt1.Columns.Add("name");
DataRow dr1=dt1.NewRow();
dr1["id"] = "A01";
dr1["name"] = "apple";
dt1.Rows.Add(dr1);
dr1 = dt1.NewRow();
dr1["id"] = "A02";
dr1["name"] = "bananer";
dt1.Rows.Add(dr1);
DataTable dt2 = new DataTable();
dt2.Columns.Add("id");
dt2.Columns.Add("QTY");
DataRow dr2 = dt2.NewRow();
dr2["id"] = "A01";
dr2["QTY"] = "100";
dt2.Rows.Add(dr2);
dr2 = dt2.NewRow();
dr2["id"] = "A02";
dr2["QTY"] = "200";
dt2.Rows.Add(dr2);
DataTable dtReslut = new DataTable();
dtReslut.Columns.Add("id");
dtReslut.Columns.Add("name");
dtReslut.Columns.Add("QTY");
var result = (from t1 in dt1.AsEnumerable()
join t2 in dt2.AsEnumerable() on t1.Field<string>("id") equals t2.Field<string>("id")
select new
{
id = t1.Field<string>("id"),
Name = t1.Field<string>("Name"),
QTY = t2.Field<string>("QTY")
});
foreach (var item in result)
{
DataRow dr = dtReslut.NewRow();
dr["id"] = item.id;
dr["name"] = item.Name;
dr["QTY"] = item.QTY;
dtReslut.Rows.Add(dr);
}
}
}
}