//---------------Wrting query for join between two data tables for Multiple Fields---------------//
var leftOuter = from datavalue1 in dtA.AsEnumerable()
join datavalue2 in dtB.AsEnumerable()
on new { Date = datavalue1["Date"], Time = datavalue1["Time"] }
equals new { Date = datavalue2["Date"], Time = datavalue2["Time"] }
into resultset
from cp in resultset.DefaultIfEmpty()
select new
{
Date = datavalue1.Field<String>("Date"),
Time = datavalue1.Field<String>("Time"),
Series1 = datavalue1 == null ? (decimal?)null : datavalue1.Field<decimal>("Value"),
Series2 = cp == null ? (decimal?)null : cp.Field<decimal>("Value"),
};
foreach (var row in leftOuter)
{
DataRow dr = dtJoin.NewRow();
dr["DateOnly"] = row.Date;
dr["TimeOnly"] = row.Time;
if (row.ColumnA1 != null)
{
dr["ColumnA"] = row.Series1;
}
else
{
dr["ColumnA"] = DBNull.Value;
}
if (row.ColumnB != null)
{
dr["ColumnB"] = row.Series2;
}
else
{
dr["ColumnB"] = DBNull.Value;
}
dtJoin.Rows.Add(dr);
var leftOuter = from datavalue1 in dtA.AsEnumerable()
join datavalue2 in dtB.AsEnumerable()
on new { Date = datavalue1["Date"], Time = datavalue1["Time"] }
equals new { Date = datavalue2["Date"], Time = datavalue2["Time"] }
into resultset
from cp in resultset.DefaultIfEmpty()
select new
{
Date = datavalue1.Field<String>("Date"),
Time = datavalue1.Field<String>("Time"),
Series1 = datavalue1 == null ? (decimal?)null : datavalue1.Field<decimal>("Value"),
Series2 = cp == null ? (decimal?)null : cp.Field<decimal>("Value"),
};
foreach (var row in leftOuter)
{
DataRow dr = dtJoin.NewRow();
dr["DateOnly"] = row.Date;
dr["TimeOnly"] = row.Time;
if (row.ColumnA1 != null)
{
dr["ColumnA"] = row.Series1;
}
else
{
dr["ColumnA"] = DBNull.Value;
}
if (row.ColumnB != null)
{
dr["ColumnB"] = row.Series2;
}
else
{
dr["ColumnB"] = DBNull.Value;
}
dtJoin.Rows.Add(dr);
No comments:
Post a Comment