#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");
dtMid.Columns.Add("date_dt", typeof(DateTime));
dtMid.Columns.Add("datavalue_n", typeof(decimal));
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++)
{
DataTable dtResult = FullOuterJoinForDataTable(dtMid, dsData.Tables[tableCount]);
dtOutput.Columns.Add("Series " + (tableCount + 1), typeof(double));
if (dtResult.Rows.Count > dtOutput.Rows.Count)
{
int extrarow = dtResult.Rows.Count - dtOutput.Rows.Count;
for (int iExtra = 0; iExtra < extrarow; iExtra++)
{
DataRow drOutput = dtOutput.NewRow();
dtOutput.Rows.Add(drOutput);
}
for (int icount = 0; icount < dtOutput.Rows.Count; icount++)
{
dtOutput.Rows[icount][0] = dtResult.Rows[icount][0];
dtOutput.Rows[icount][tableCount + 1] = dtResult.Rows[icount]["Series 2"];
}
}
else
{
for (int icount = 0; icount < dtResult.Rows.Count; icount++)
{
dtOutput.Rows[icount][tableCount + 1] = dtResult.Rows[icount]["Series 2"];
}
}
}
}
return dtOutput;
}
#endregion
#region Join DataTable for DatesOutPut
public DataTable JoinDatatableForDates(DataTable dt1RainFall, DataTable dt2MaxTemprature, DataTable dt3MinTemprature)
{
DataTable dtDateOutPut = new DataTable("dtDateOutPut");
try
{
dtDateOutPut.Columns.Add("date_dt", typeof(DateTime));
var result = from dataRows1 in dt1RainFall.AsEnumerable()
join dataRows2 in dt2MaxTemprature.AsEnumerable() on dataRows1.Field<DateTime>("date_dt") equals dataRows2.Field<DateTime>("date_dt")
join dataRows3 in dt3MinTemprature.AsEnumerable() on dataRows1.Field<DateTime>("date_dt") equals dataRows3.Field<DateTime>("date_dt")
select dtDateOutPut.LoadDataRow(new object[]
{
dataRows1.Field<DateTime>("date_dt")
}, false);
//---------------copy output of result into datatable named 'dtDateOutPut'---------------//
if (result != null && result.Count() > 0)
{
dtDateOutPut = result.CopyToDataTable();
}
}
catch (Exception ex)
{
LoggerClass.ErrorException(ex.Message, ex);
}
return dtDateOutPut;
}
#endregion
#region Join DataTable for Common Date with Values
public DataTable JoinDatatableForCommonData(DataTable dtAllDate, DataTable dt1, DataTable dt2, DataTable dt3)
{
DataTable dtCommonDataOutPut = new DataTable("dtCommonDataOutPut");
try
{
dtCommonDataOutPut.Columns.Add("date_dt", typeof(DateTime));
dtCommonDataOutPut.Columns.Add("Rainfall", typeof(decimal));
dtCommonDataOutPut.Columns.Add("MaxTemprature", typeof(decimal));
dtCommonDataOutPut.Columns.Add("MinTemprature", typeof(decimal));
var result = from dataRowsdtAllDate in dtAllDate.AsEnumerable()
join dataRows1 in dt1.AsEnumerable() on dataRowsdtAllDate.Field<DateTime>("date_dt") equals dataRows1.Field<DateTime>("date_dt")
join dataRows2 in dt2.AsEnumerable() on dataRowsdtAllDate.Field<DateTime>("date_dt") equals dataRows2.Field<DateTime>("date_dt")
join dataRows3 in dt3.AsEnumerable() on dataRowsdtAllDate.Field<DateTime>("date_dt") equals dataRows3.Field<DateTime>("date_dt")
select dtCommonDataOutPut.LoadDataRow(new object[]
{
dataRows1.Field<DateTime>("date_dt"),
dataRows1.Field<decimal?>("datavalue_n"),
dataRows2.Field<decimal?>("datavalue_n"),
dataRows3.Field<decimal?>("datavalue_n")
}, false);
//---------------copy output of result into datatable named 'dtCommonDataOutPut'---------------//
if (result != null && result.Count() > 0)
{
dtCommonDataOutPut = result.CopyToDataTable();
}
}
catch (Exception ex)
{
LoggerClass.ErrorException(ex.Message, ex);
}
return dtCommonDataOutPut;
}
#endregion