Tuesday 22 July 2014

Computing the average in a column in C#

 for (int iYear = iStartYear; iYear <= iEndYear; iYear++)
            {
                for (int iMonth = 1; iMonth <= 12; iMonth++)
                {
                    double dAvgTmpMax = 0.0;
                    double dAvgTmpMin = 0.0;
                 
                    //dMonthWiseSum = Convert.ToDouble(dtCommonValuesAfterJoin.Compute("AVG(MaxTemprature)", iMonth.ToString()));

                    dAvgTmpMax = Convert.ToDouble(dtCommonValuesAfterJoin.AsEnumerable()
                        .Where(d => d.Field<DateTime>("date_dt").Month == iMonth && d.Field<DateTime>("date_dt").Year == iYear)
                            .Average(r =>
                                r.Field<decimal>("MaxTemprature")));

                    dAvgTmpMin = Convert.ToDouble(dtCommonValuesAfterJoin.AsEnumerable()
                       .Where(d => d.Field<DateTime>("date_dt").Month == iMonth && d.Field<DateTime>("date_dt").Year == iYear)
                           .Average(r =>
                               r.Field<decimal>("MinTemprature")));

                    dtCalculatedOutPut.Rows[iRow]["AvgTmpMax"] = Math.Round(dAvgTmpMax, 2);
                    dtCalculatedOutPut.Rows[iRow]["AvgTmpMin"] = Math.Round(dAvgTmpMin, 2);
                    iRow++;
                }
            }

No comments:

Post a Comment