Monday 13 April 2015

join between 3 datatable in C#

  #region Join 3 DataTable for Common Date with Values
        public DataTable JoinDatatableForCommonData(DataTable dtTSeries, DataTable dtPcpSeries, DataTable dtSnSeries)
        {
            DataTable dtOutput = new DataTable("dtOutput");
            try
            {
                //dt.Columns.Add("Station Detail Id", typeof(string));
                dtOutput.Columns.Add("Date", typeof(string));
                dtOutput.Columns.Add("month_year", typeof(string));
                dtOutput.Columns.Add("Series1", typeof(decimal));
                dtOutput.Columns.Add("Series2", typeof(decimal));
                dtOutput.Columns.Add("Series3", typeof(decimal));

                var result = from dtT in dtTSeries.AsEnumerable()
                             join dtPcp in dtPcpSeries.AsEnumerable()
                             on dtT.Field<string>("month_year") equals dtPcp.Field<string>("month_year")
                             join dtSn in dtSnSeries.AsEnumerable()
                             on dtT.Field<string>("month_year") equals dtSn.Field<string>("month_year")


                             select dtOutput.LoadDataRow(new object[]
                            {
                              dtT.Field<string>("Date"),
                              dtT.Field<string>("month_year"),
                              dtT.Field<decimal?>("Value"),
                              dtPcp.Field<decimal?>("Value"),
                              dtSn.Field<decimal?>("Value")
                         
                            }, false);
                //---------------copy output of result into datatable named 'dtOutput'---------------//

                if (result != null && result.Count() > 0)
                {
                    dtOutput = result.CopyToDataTable();
                }
            }
            catch (Exception ex)
            {
                LoggerClass.ErrorException(ex.Message, ex);
            }
            return dtOutput;
        }
        #endregion

group the data in grid in C#

  #region Group the GridValue
        public DataTable GroupGrid(DataTable dt)
        {
            DataTable dtA = new DataTable();
            dtA = dt.Clone();
            string strCurrentValue = string.Empty;
            string strPreviousValue = string.Empty;
            int m;
            int n = 1;
            for (m = 0; m < dt.Rows.Count; m++)
            {
                if (n == 1)   // for first value
                {
                    DataRow dr = dtA.NewRow();
                    dr["Select"] = false;
                    dr["season"] = dt.Rows[0]["season"].ToString();         //season-> month/monsoon
                    dr["cluster_no"] = dt.Rows[0]["cluster_no"].ToString();
                    dr["equation"] = dt.Rows[0]["equation"].ToString();

                    dtA.Rows.Add(dr);
                }
                if (n > 1)
                {
                    if (m == 1)
                    {
                        object cellValuePrevious = dt.Rows[m]["season"];
                        strPreviousValue = cellValuePrevious.ToString().Trim();
                    }

                    object cellValue = dt.Rows[m]["season"];
                    strCurrentValue = cellValue.ToString().Trim();

                    if (strCurrentValue != strPreviousValue) // Now compare the current value with previous value
                    {
                        DataRow dr = dtA.NewRow();
                        dr["Select"] = false;
                        dr["season"] = dt.Rows[m]["season"].ToString();
                        dr["cluster_no"] = dt.Rows[m]["cluster_no"].ToString();
                        dr["equation"] = dt.Rows[m]["equation"].ToString();
                        dtA.Rows.Add(dr);
                        strPreviousValue = strCurrentValue;
                    }
                    else
                    {
                        DataRow dr = dtA.NewRow();
                        dr["Select"] = false;
                        dr["season"] = string.Empty;
                        dr["cluster_no"] = dt.Rows[m]["cluster_no"].ToString();
                        dr["equation"] = dt.Rows[m]["equation"].ToString();
                        dtA.Rows.Add(dr);
                        strPreviousValue = strCurrentValue;
                    }
                }
                n++;
            }
            return dtA;
        }
        #endregion

select value with where condition in C# linq

var riverId = _dtRiverList.AsEnumerable()
                                  .Where(r => r.Field<string>("river_name").Equals(basin))
                                  .Select(r => r.Field<Int32>("river_id"));
                if (riverId.Count() > 0)
                {
                    _dtSubBasin = new DataTable("_dtSubBasin");
                    objRModelingDAL.BasinId = riverId.ElementAt(0);
                    _dtSubBasin = objRModelingDAL.GetSubBasin();
                }

how to import row in C# datatable

 #region  Get Basin
        public DataTable GetRiver()
        {
            try
            {
                _dtRiverList = new DataTable("_dtRiverList");
                DataTable dt = objRModelingDAL.GetRiverList();
                _dtRiverList = dt.Clone();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (
                        (Convert.ToInt32(dt.Rows[i]["river_id"]) == 8) || (Convert.ToInt32(dt.Rows[i]["river_id"]) == 9) ||
                        (Convert.ToInt32(dt.Rows[i]["river_id"]) == 20) || (Convert.ToInt32(dt.Rows[i]["river_id"]) == 25) ||
                        (Convert.ToInt32(dt.Rows[i]["river_id"]) == 26) || (Convert.ToInt32(dt.Rows[i]["river_id"]) == 27) ||
                        (Convert.ToInt32(dt.Rows[i]["river_id"]) == 28)
                        )
                    {
                        _dtRiverList.ImportRow(dt.Rows[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerClass.ErrorException(ex.Message, ex);
            }
            return _dtRiverList;
        }
        #endregion