#region Join for Single and Multiple Tables using dataSet
//----------Join for single and Multiple Tables----------//
public DataTable JoinDataTables(DataSet dsData)
{
DataTable dtOutput = new DataTable("dtOutput");
if (dsData.Tables.Count.Equals(1))
{
dtOutput = dsData.Tables[0];
}
else if (dsData.Tables.Count.Equals(2))
{
dtOutput = FullOuterJoinForDataTable(dsData.Tables[0], dsData.Tables[1]);
}
else if (dsData.Tables.Count > 2)
{
dtOutput = FullOuterJoinForDataTable(dsData.Tables[0], dsData.Tables[1]);
DataTable dtMid = new DataTable("dtMid");
if (dtOutput != null && dtOutput.Rows.Count > 0)
{
dtMid.Columns.Add("date_dt", typeof(DateTime));
dtMid.Columns.Add("datavalue_n", typeof(double));
foreach (DataRow drFinal in dtOutput.Rows)
{
DataRow drMid = dtMid.NewRow();
drMid[0] = drFinal.ItemArray[0];
drMid[1] = drFinal.ItemArray[1];
dtMid.Rows.Add(drMid);
}
}
for (int tableCount = 2; tableCount < dsData.Tables.Count; tableCount++)
{
if (dtMid != null || dtMid.Rows.Count > 0)
{
DataTable dtResult = FullOuterJoinForDataTable(dtMid, dsData.Tables[tableCount]);
if (dtResult != null && dtResult.Rows.Count > 0)
{
dtOutput.Columns.Add("Series " + (tableCount + 1), typeof(double));
for (int i = 0; i < dtResult.Rows.Count; i++)
{
dtOutput.Rows[i]["Series " + (tableCount + 1)] = dtResult.Rows[i][dtResult.Columns.Count - 1];
}
}
}
}
}
return dtOutput;
}
#endregion
#region INNER JOIN for Common Date data
public DataTable FullOuterJoinForDataTable(DataTable dtParam1, DataTable dtParam2)
{
DataTable dtOutput = new DataTable("dtOutput");
//dt.Columns.Add("Station Detail Id", typeof(string));
dtOutput.Columns.Add("date_dt", typeof(DateTime));
dtOutput.Columns.Add("Series 1", typeof(double));
dtOutput.Columns.Add("Series 2", typeof(double));
var result = from datavalue1 in dtParam1.AsEnumerable()
join datavalue2 in dtParam2.AsEnumerable()
on datavalue1.Field<DateTime>("date_dt")
equals datavalue2.Field<DateTime>("date_dt")
select dtOutput.LoadDataRow(new object[]
{
datavalue1.Field<DateTime>("date_dt"),
datavalue1.Field<double?>("datavalue_n"),
datavalue2.Field<double?>("datavalue_n")
}, false);
//---------------copy output of result into datatable named 'dtCommonDataOutPut'---------------//
if (result != null && result.Count() > 0)
{
dtOutput = result.CopyToDataTable();
}
return dtOutput;
}
#endregion
//----------Join for single and Multiple Tables----------//
public DataTable JoinDataTables(DataSet dsData)
{
DataTable dtOutput = new DataTable("dtOutput");
if (dsData.Tables.Count.Equals(1))
{
dtOutput = dsData.Tables[0];
}
else if (dsData.Tables.Count.Equals(2))
{
dtOutput = FullOuterJoinForDataTable(dsData.Tables[0], dsData.Tables[1]);
}
else if (dsData.Tables.Count > 2)
{
dtOutput = FullOuterJoinForDataTable(dsData.Tables[0], dsData.Tables[1]);
DataTable dtMid = new DataTable("dtMid");
if (dtOutput != null && dtOutput.Rows.Count > 0)
{
dtMid.Columns.Add("date_dt", typeof(DateTime));
dtMid.Columns.Add("datavalue_n", typeof(double));
foreach (DataRow drFinal in dtOutput.Rows)
{
DataRow drMid = dtMid.NewRow();
drMid[0] = drFinal.ItemArray[0];
drMid[1] = drFinal.ItemArray[1];
dtMid.Rows.Add(drMid);
}
}
for (int tableCount = 2; tableCount < dsData.Tables.Count; tableCount++)
{
if (dtMid != null || dtMid.Rows.Count > 0)
{
DataTable dtResult = FullOuterJoinForDataTable(dtMid, dsData.Tables[tableCount]);
if (dtResult != null && dtResult.Rows.Count > 0)
{
dtOutput.Columns.Add("Series " + (tableCount + 1), typeof(double));
for (int i = 0; i < dtResult.Rows.Count; i++)
{
dtOutput.Rows[i]["Series " + (tableCount + 1)] = dtResult.Rows[i][dtResult.Columns.Count - 1];
}
}
}
}
}
return dtOutput;
}
#endregion
#region INNER JOIN for Common Date data
public DataTable FullOuterJoinForDataTable(DataTable dtParam1, DataTable dtParam2)
{
DataTable dtOutput = new DataTable("dtOutput");
//dt.Columns.Add("Station Detail Id", typeof(string));
dtOutput.Columns.Add("date_dt", typeof(DateTime));
dtOutput.Columns.Add("Series 1", typeof(double));
dtOutput.Columns.Add("Series 2", typeof(double));
var result = from datavalue1 in dtParam1.AsEnumerable()
join datavalue2 in dtParam2.AsEnumerable()
on datavalue1.Field<DateTime>("date_dt")
equals datavalue2.Field<DateTime>("date_dt")
select dtOutput.LoadDataRow(new object[]
{
datavalue1.Field<DateTime>("date_dt"),
datavalue1.Field<double?>("datavalue_n"),
datavalue2.Field<double?>("datavalue_n")
}, false);
//---------------copy output of result into datatable named 'dtCommonDataOutPut'---------------//
if (result != null && result.Count() > 0)
{
dtOutput = result.CopyToDataTable();
}
return dtOutput;
}
#endregion
No comments:
Post a Comment