Exemple #1
0
        public IEnumerable<PowerViewModel> Get_Power_Log_Month(int? targetMonth, int? targetYear)
        {
            if (targetMonth == null)
                targetMonth = DateTime.Today.Month;
            if (targetYear == null)
                targetYear = DateTime.Today.Year;
            int nDay = DateTime.DaysInMonth(targetYear ?? DateTime.Today.Year, targetMonth ?? DateTime.Today.Month);
            int nDayLastMonth = 0;
            if(targetMonth != null && targetYear != null)
            {
                if (targetMonth == 1)
                    nDayLastMonth = DateTime.DaysInMonth((int)targetYear - 1, 12);
                else
                    nDayLastMonth = DateTime.DaysInMonth((int)targetYear, (int)targetMonth - 1);
            }
            else
            {
                if(DateTime.Today.Month == 1)
                    nDayLastMonth = DateTime.DaysInMonth(DateTime.Today.Year-1, 12);
                else
                    nDayLastMonth = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month - 1);
            }
            var queryDoingam = from x in db.doingam_log1hr
                               where (SqlFunctions.DatePart("month", x.LocalCol.Value) == targetMonth
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetYear
                                    && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8)
                                    || (
                                            SqlFunctions.DatePart("day", x.LocalCol.Value) == nDayLastMonth
                                            && SqlFunctions.DatePart("month", x.LocalCol.Value) == ((targetMonth==1)? 12 : targetMonth - 1)
                                            && SqlFunctions.DatePart("year", x.LocalCol.Value) == ((targetMonth==1)? targetYear-1 : targetYear)
                                            && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8
                                        )
                               select new dayViewModel
                               {
                                   day = x.LocalCol.Value.Day,
                                   energy = (x.Doingam_MBInDataEnergy??0)/10
                               };
            var queryHigh1 = from x in db.high1_log1hr
                             where (SqlFunctions.DatePart("month", x.LocalCol.Value) == targetMonth
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetYear
                                    && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8)
                                    || (
                                            SqlFunctions.DatePart("day", x.LocalCol.Value) == nDayLastMonth
                                            && SqlFunctions.DatePart("month", x.LocalCol.Value) == ((targetMonth == 1) ? 12 : targetMonth - 1)
                                            && SqlFunctions.DatePart("year", x.LocalCol.Value) == ((targetMonth == 1) ? targetYear - 1 : targetYear)
                                            && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8
                                        )
                             select new dayViewModel
                             {
                                 day = x.LocalCol.Value.Day,
                                 energy = x.High1_DataEnergy??0
                             };
            var queryHigh2 = from x in db.high2_log1hr
                             where (SqlFunctions.DatePart("month", x.LocalCol.Value) == targetMonth
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetYear
                                    && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8)
                                    || (
                                            SqlFunctions.DatePart("day", x.LocalCol.Value) == nDayLastMonth
                                            && SqlFunctions.DatePart("month", x.LocalCol.Value) == ((targetMonth == 1) ? 12 : targetMonth - 1)
                                            && SqlFunctions.DatePart("year", x.LocalCol.Value) == ((targetMonth == 1) ? targetYear - 1 : targetYear)
                                            && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8
                                        )
                             select new dayViewModel
                             {
                                 day = x.LocalCol.Value.Day,
                                 energy = x.High2_DataEnergy??0
                             };
            var queryHuainamrin = from x in db.huainamrin_log1hr
                                  where (SqlFunctions.DatePart("month", x.LocalCol.Value) == targetMonth
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetYear
                                    && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8)
                                    || (
                                            SqlFunctions.DatePart("day", x.LocalCol.Value) == nDayLastMonth
                                            && SqlFunctions.DatePart("month", x.LocalCol.Value) == ((targetMonth == 1) ? 12 : targetMonth - 1)
                                            && SqlFunctions.DatePart("year", x.LocalCol.Value) == ((targetMonth == 1) ? targetYear - 1 : targetYear)
                                            && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8
                                        )
                                  select new dayViewModel
                                  {
                                      day = x.LocalCol.Value.Day,
                                      energy = (x.Huaynamrin_DataEnergy ?? 0) / 10
                                  };
            var queryRawwater = from x in db.low_log1hr
                                where (SqlFunctions.DatePart("month", x.LocalCol.Value) == targetMonth
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetYear
                                    && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8)
                                    || (
                                            SqlFunctions.DatePart("day", x.LocalCol.Value) == nDayLastMonth
                                            && SqlFunctions.DatePart("month", x.LocalCol.Value) == ((targetMonth == 1) ? 12 : targetMonth - 1)
                                            && SqlFunctions.DatePart("year", x.LocalCol.Value) == ((targetMonth == 1) ? targetYear - 1 : targetYear)
                                            && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8
                                        )
                                select new dayViewModel
                                {
                                    day = x.LocalCol.Value.Day,
                                    energy = (x.RawWater_DataEnergy ?? 0) / 10
                                };
            var queryPhajome = from x in db.pharjome_log1hr
                               where (SqlFunctions.DatePart("month", x.LocalCol.Value) == targetMonth
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetYear
                                    && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8)
                                    || (
                                            SqlFunctions.DatePart("day", x.LocalCol.Value) == nDayLastMonth
                                            && SqlFunctions.DatePart("month", x.LocalCol.Value) == ((targetMonth == 1) ? 12 : targetMonth - 1)
                                            && SqlFunctions.DatePart("year", x.LocalCol.Value) == ((targetMonth == 1) ? targetYear - 1 : targetYear)
                                            && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8
                                        )
                               select new dayViewModel
                               {
                                   day = x.LocalCol.Value.Day,
                                   energy = (x.Phajome_DataEnergy ?? 0) / 10
                               };
            var queryPhom = from x in db.phom_log1hr
                            where (SqlFunctions.DatePart("month", x.LocalCol.Value) == targetMonth
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetYear
                                    && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8)
                                    || (
                                            SqlFunctions.DatePart("day", x.LocalCol.Value) == nDayLastMonth
                                            && SqlFunctions.DatePart("month", x.LocalCol.Value) == ((targetMonth == 1) ? 12 : targetMonth - 1)
                                            && SqlFunctions.DatePart("year", x.LocalCol.Value) == ((targetMonth == 1) ? targetYear - 1 : targetYear)
                                            && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8
                                        )
                            select new dayViewModel
                            {
                                day = x.LocalCol.Value.Day,
                                energy = (x.Phomvihane_DataEnergy ?? 0) / 10
                            };
            var querySoonchar = from x in db.soonchar_log1hr
                                where (SqlFunctions.DatePart("month", x.LocalCol.Value) == targetMonth
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetYear
                                    && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8)
                                    || (
                                            SqlFunctions.DatePart("day", x.LocalCol.Value) == nDayLastMonth
                                            && SqlFunctions.DatePart("month", x.LocalCol.Value) == ((targetMonth == 1) ? 12 : targetMonth - 1)
                                            && SqlFunctions.DatePart("year", x.LocalCol.Value) == ((targetMonth == 1) ? targetYear - 1 : targetYear)
                                            && SqlFunctions.DatePart("hour", x.LocalCol.Value) == 8
                                        )
                                select new dayViewModel
                                {
                                    day = x.LocalCol.Value.Day,
                                    energy = (x.Sooncha_DataEnergy ?? 0) / 10
                                };
            List<dayViewModel> dayList = new List<dayViewModel>();
            for (int i = 1; i <= nDay; i++)
            {
                dayList.Add(new dayViewModel { day = i, energy = 0 });
            }
            PowerViewModel sumEnergy = new PowerViewModel();
            try
            {
                // Substract
                var doingamList = queryDoingam.ToList();
                if(doingamList.Count > 1)
                {
                    for (int i = doingamList.Count - 1; i > 0; i--)
                    {
                        if (doingamList[i].energy >= doingamList[i - 1].energy)
                            doingamList[i].diffEnergy = doingamList[i].energy - doingamList[i - 1].energy;
                        else
                            doingamList[i].diffEnergy = 0;
                    }
                    if (doingamList[1].day < doingamList[0].day)
                        doingamList.RemoveAt(0);
                }

                var high1List = queryHigh1.ToList();
                if(high1List.Count > 1)
                {
                    for (int i = high1List.Count - 1; i > 0; i--)
                    {
                        if (high1List[i].energy >= high1List[i - 1].energy)
                            high1List[i].diffEnergy = high1List[i].energy - high1List[i - 1].energy;
                        else
                            high1List[i].diffEnergy = 0;
                    }
                    if (high1List[1].day < high1List[0].day)
                        high1List.RemoveAt(0);
                }

                var high2List = queryHigh2.ToList();
                if(high2List.Count > 1)
                {
                    for (int i = high2List.Count - 1; i > 0; i--)
                    {
                        if (high2List[i].energy >= high2List[i - 1].energy)
                            high2List[i].diffEnergy = high2List[i].energy - high2List[i - 1].energy;
                        else
                            high2List[i].diffEnergy = 0;
                    }
                    if (high2List[1].day < high2List[0].day)
                        high2List.RemoveAt(0);
                }

                var huainamrinList = queryHuainamrin.ToList();
                if(huainamrinList.Count > 1)
                {
                    for (int i = huainamrinList.Count - 1; i > 0; i--)
                    {
                        if (huainamrinList[i].energy >= huainamrinList[i - 1].energy)
                            huainamrinList[i].diffEnergy = huainamrinList[i].energy - huainamrinList[i - 1].energy;
                        else
                            huainamrinList[i].diffEnergy = 0;
                    }
                    if (huainamrinList[1].day < huainamrinList[0].day)
                        huainamrinList.RemoveAt(0);
                }

                var rawwaterList = queryRawwater.ToList();
                if(rawwaterList.Count > 1)
                {
                    for (int i = rawwaterList.Count - 1; i > 0; i--)
                    {
                        if (rawwaterList[i].energy >= rawwaterList[i - 1].energy)
                            rawwaterList[i].diffEnergy = rawwaterList[i].energy - rawwaterList[i - 1].energy;
                        else
                            rawwaterList[i].diffEnergy = 0;
                    }
                    if (rawwaterList[1].day < rawwaterList[0].day)
                        rawwaterList.RemoveAt(0);
                }

                var phajomeList = queryPhajome.ToList();
                if(phajomeList.Count > 1)
                {
                    for (int i = phajomeList.Count - 1; i > 0; i--)
                    {
                        if (phajomeList[i].energy >= phajomeList[i - 1].energy)
                            phajomeList[i].diffEnergy = phajomeList[i].energy - phajomeList[i - 1].energy;
                        else
                            phajomeList[i].diffEnergy = 0;
                    }
                    if (phajomeList[1].day < phajomeList[0].day)
                        phajomeList.RemoveAt(0);
                }

                var phomList = queryPhom.ToList();
                if(phomList.Count > 1 )
                {
                    for (int i = phomList.Count - 1; i > 0; i--)
                    {
                        if (phomList[i].energy >= phomList[i - 1].energy)
                            phomList[i].diffEnergy = phomList[i].energy - phomList[i - 1].energy;
                        else
                            phomList[i].diffEnergy = 0;
                    }
                    if (phomList[1].day < phomList[0].day)
                        phomList.RemoveAt(0);
                }

                var sooncharList = querySoonchar.ToList();
                if(sooncharList.Count > 1)
                {
                    for (int i = sooncharList.Count - 1; i > 0; i--)
                    {
                        if (sooncharList[i].energy >= sooncharList[i - 1].energy)
                            sooncharList[i].diffEnergy = sooncharList[i].energy - sooncharList[i - 1].energy;
                        else
                            sooncharList[i].diffEnergy = 0;
                    }
                    if (sooncharList[1].day < sooncharList[0].day)
                        sooncharList.RemoveAt(0);
                }

                var list = from x in dayList
                           join y1 in rawwaterList on x.day equals y1.day into g1
                           from t1 in g1.Where(k => k.day == x.day).DefaultIfEmpty()
                           join y2 in doingamList on x.day equals y2.day into g2
                           from t2 in g2.Where(k => k.day == x.day).DefaultIfEmpty()
                           join y3 in high1List on x.day equals y3.day into g3
                           from t3 in g3.Where(k => k.day == x.day).DefaultIfEmpty()
                           join y4 in high2List on x.day equals y4.day into g4
                           from t4 in g4.Where(k => k.day == x.day).DefaultIfEmpty()
                           join y5 in huainamrinList on x.day equals y5.day into g5
                           from t5 in g5.Where(k => k.day == x.day).DefaultIfEmpty()
                           join y6 in phajomeList on x.day equals y6.day into g6
                           from t6 in g6.Where(k => k.day == x.day).DefaultIfEmpty()
                           join y7 in phomList on x.day equals y7.day into g7
                           from t7 in g7.Where(k => k.day == x.day).DefaultIfEmpty()
                           join y8 in sooncharList on x.day equals y8.day into g8
                           from t8 in g8.Where(k => k.day == x.day).DefaultIfEmpty()
                           select new PowerViewModel
                           {
                               timeStamp = x.day.ToString(),
                               rawWater = t1 == null ? 0.00 : Math.Round((double)t1.energy, 2),
                               rawWaterDiff = t1 == null ? 0.00 : Math.Round((double)t1.diffEnergy, 2),
                               doiNgam = t2 == null ? 0.00 : Math.Round((double)t2.energy, 2),
                               doiNgamDiff = t2 == null ? 0.00 : Math.Round((double)t2.diffEnergy, 2),
                               high1 = t3 == null ? 0.00 : Math.Round((double)t3.energy, 2),
                               high1Diff = t3 == null ? 0.00 : Math.Round((double)t3.diffEnergy, 2),
                               high2 = t4 == null ? 0.00 : Math.Round((double)t4.energy, 2),
                               high2Diff = t4 == null ? 0.00 : Math.Round((double)t4.diffEnergy, 2),
                               huaiNamrin = t5 == null ? 0.00 : Math.Round((double)t5.energy, 2),
                               huaiNamrinDiff = t5 == null ? 0.00 : Math.Round((double)t5.diffEnergy, 2),
                               phaJome = t6 == null ? 0.00 : Math.Round((double)t6.energy, 2),
                               phaJomeDiff = t6 == null ? 0.00 : Math.Round((double)t6.diffEnergy, 2),
                               phom = t7 == null ? 0.00 : Math.Round((double)t7.energy, 2),
                               phomDiff = t7 == null ? 0.00 : Math.Round((double)t7.diffEnergy, 2),
                               soonChar = t8 == null ? 0.00 : Math.Round((double)t8.energy, 2),
                               soonCharDiff = t8 == null ? 0.00 : Math.Round((double)t8.diffEnergy, 2),
                           };
                List<PowerViewModel> newList = new List<PowerViewModel>();
                newList = list.ToList();
                for (int i = 0; i<newList.Count; i++)
                {
                    sumEnergy.timeStamp = "Total";
                    sumEnergy.doiNgamDiff = Math.Round(sumEnergy.doiNgamDiff + newList[i].doiNgamDiff, 2);
                    sumEnergy.rawWaterDiff = Math.Round(sumEnergy.rawWaterDiff + newList[i].rawWaterDiff, 2);
                    sumEnergy.high1Diff = Math.Round(sumEnergy.high1Diff + newList[i].high1Diff, 2);
                    sumEnergy.high2Diff = Math.Round(sumEnergy.high2Diff + newList[i].high2Diff, 2);
                    sumEnergy.huaiNamrinDiff = Math.Round(sumEnergy.huaiNamrinDiff + newList[i].huaiNamrinDiff, 2);
                    sumEnergy.phaJomeDiff = Math.Round(sumEnergy.phaJomeDiff + newList[i].phaJomeDiff, 2);
                    sumEnergy.phomDiff = Math.Round(sumEnergy.phomDiff + newList[i].phomDiff, 2);
                    sumEnergy.soonCharDiff = Math.Round(sumEnergy.soonCharDiff + newList[i].soonCharDiff, 2);
                }
                sumEnergy.doiNgam = (doingamList.Count > 1) ? Math.Round(doingamList[doingamList.Count - 1].energy,2) : 0;
                sumEnergy.rawWater = (rawwaterList.Count > 1) ? Math.Round(rawwaterList[rawwaterList.Count - 1].energy,2) : 0;
                sumEnergy.high1 = (high1List.Count > 1) ? Math.Round(high1List[high1List.Count - 1].energy,2) : 0;
                sumEnergy.high2 = (high2List.Count > 1) ? Math.Round(high2List[high2List.Count - 1].energy,2) : 0;
                sumEnergy.huaiNamrin = (huainamrinList.Count > 1) ? Math.Round(huainamrinList[huainamrinList.Count - 1].energy,2) : 0;
                sumEnergy.phaJome = (phajomeList.Count > 1) ? Math.Round(phajomeList[phajomeList.Count - 1].energy,2) : 0;
                sumEnergy.phom = (phomList.Count > 1) ? Math.Round(phomList[phomList.Count - 1].energy,2) : 0;
                sumEnergy.soonChar = (sooncharList.Count > 1) ? Math.Round(sooncharList[sooncharList.Count - 1].energy,2) : 0;
                newList.Add(sumEnergy);
                return newList;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return null;
            }
        }
Exemple #2
0
        public IEnumerable<PowerViewModel> Get_Power_Log(DateTime targetDate)
        {
            if (targetDate == null)
                targetDate = DateTime.Today;
            var queryDoingam = from x in db.doingam_log1hr
                           where SqlFunctions.DatePart("day", x.LocalCol.Value) == SqlFunctions.DatePart("day", targetDate.Date)
                            && SqlFunctions.DatePart("month", x.LocalCol.Value) == targetDate.Month
                            && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetDate.Year
                           orderby x.LocalCol ascending
                           select new
                           {
                               hr = x.LocalCol.Value.Hour,
                               energy = x.Doingam_MBInDataEnergy / 10
                           };
            var queryHigh1 = from x in db.high1_log1hr
                                where SqlFunctions.DatePart("day", x.LocalCol.Value) == SqlFunctions.DatePart("day", targetDate.Date)
                                 && SqlFunctions.DatePart("month", x.LocalCol.Value) == targetDate.Month
                                 && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetDate.Year
                             orderby x.LocalCol ascending
                                select new
                                {
                                    hr = x.LocalCol.Value.Hour,
                                    energy = x.High1_DataEnergy
                                };
            var queryHigh2 = from x in db.high2_log1hr
                             where SqlFunctions.DatePart("day", x.LocalCol.Value) == SqlFunctions.DatePart("day", targetDate.Date)
                                && SqlFunctions.DatePart("month", x.LocalCol.Value) == targetDate.Month
                                && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetDate.Year
                             orderby x.LocalCol ascending
                             select new
                             {
                                 hr = x.LocalCol.Value.Hour,
                                 energy = x.High2_DataEnergy
                             };
            var queryHuainamrin = from x in db.huainamrin_log1hr
                                where SqlFunctions.DatePart("day", x.LocalCol.Value) == SqlFunctions.DatePart("day", targetDate.Date)
                                    && SqlFunctions.DatePart("month", x.LocalCol.Value) == targetDate.Month
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetDate.Year
                                  orderby x.LocalCol ascending
                                select new
                                {
                                    hr = x.LocalCol.Value.Hour,
                                    energy = x.Huaynamrin_DataEnergy / 10
                                };
            var queryRawwater = from x in db.low_log1hr
                                  where SqlFunctions.DatePart("day", x.LocalCol.Value) == SqlFunctions.DatePart("day", targetDate.Date)
                                    && SqlFunctions.DatePart("month", x.LocalCol.Value) == targetDate.Month
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetDate.Year
                                orderby x.LocalCol ascending
                                  select new
                                  {
                                      hr = x.LocalCol.Value.Hour,
                                      energy = x.RawWater_DataEnergy
                                  };
            var queryPhajome = from x in db.pharjome_log1hr
                                where SqlFunctions.DatePart("day", x.LocalCol.Value) == SqlFunctions.DatePart("day", targetDate.Date)
                                    && SqlFunctions.DatePart("month", x.LocalCol.Value) == targetDate.Month
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetDate.Year
                               orderby x.LocalCol ascending
                                select new
                                {
                                    hr = x.LocalCol.Value.Hour,
                                    energy = x.Phajome_DataEnergy / 10
                                };
            var queryPhom = from x in db.phom_log1hr
                               where SqlFunctions.DatePart("day", x.LocalCol.Value) == SqlFunctions.DatePart("day", targetDate.Date)
                                    && SqlFunctions.DatePart("month", x.LocalCol.Value) == targetDate.Month
                                    && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetDate.Year
                            orderby x.LocalCol ascending
                               select new
                               {
                                   hr = x.LocalCol.Value.Hour,
                                   energy = x.Phomvihane_DataEnergy / 10
                               };
            var querySoonchar = from x in db.soonchar_log1hr
                            where SqlFunctions.DatePart("day", x.LocalCol.Value) == SqlFunctions.DatePart("day", targetDate.Date)
                                && SqlFunctions.DatePart("month", x.LocalCol.Value) == targetDate.Month
                                && SqlFunctions.DatePart("year", x.LocalCol.Value) == targetDate.Year
                                orderby x.LocalCol ascending
                            select new
                            {
                                hr = x.LocalCol.Value.Hour,
                                energy = x.Sooncha_DataEnergy / 10
                            };
            List<HrViewModel> hrList = new List<HrViewModel>();
            for(int i=0; i<=23; i++)
            {
                hrList.Add(new HrViewModel { hr = i, energy = 0 });
            }

            try
            {
                var doingamList = queryDoingam.ToList();
                var high1List = queryHigh1.ToList();
                var high2List = queryHigh2.ToList();
                var huainamrinList = queryHuainamrin.ToList();
                var rawwaterList = queryRawwater.ToList();
                var phajomeList = queryPhajome.ToList();
                var phomList = queryPhom.ToList();
                var sooncharList = querySoonchar.ToList();
                var list = from x in hrList
                           join y1 in rawwaterList on x.hr equals y1.hr into g1
                           from t1 in g1.Where(k => k.hr == x.hr).DefaultIfEmpty()
                           join y2 in doingamList on x.hr equals y2.hr into g2
                           from t2 in g2.Where(k => k.hr == x.hr).DefaultIfEmpty()
                           join y3 in high1List on x.hr equals y3.hr into g3
                           from t3 in g3.Where(k => k.hr == x.hr).DefaultIfEmpty()
                           join y4 in high2List on x.hr equals y4.hr into g4
                           from t4 in g4.Where(k => k.hr == x.hr).DefaultIfEmpty()
                           join y5 in huainamrinList on x.hr equals y5.hr into g5
                           from t5 in g5.Where(k => k.hr == x.hr).DefaultIfEmpty()
                           join y6 in phajomeList on x.hr equals y6.hr into g6
                           from t6 in g6.Where(k => k.hr == x.hr).DefaultIfEmpty()
                           join y7 in phomList on x.hr equals y7.hr into g7
                           from t7 in g7.Where(k => k.hr == x.hr).DefaultIfEmpty()
                           join y8 in sooncharList on x.hr equals y8.hr into g8
                           from t8 in g8.Where(k => k.hr == x.hr).DefaultIfEmpty()
                           select new PowerViewModel
                           {
                               timeStamp    = x.hr.ToString() + ":00",
                               rawWater     = t1 == null ? 0.00 : (double)t1.energy,
                               doiNgam      = t2 == null ? 0.00 : (double)t2.energy,
                               high1        = t3 == null ? 0.00 : (double)t3.energy,
                               high2        = t4 == null ? 0.00 : (double)t4.energy,
                               huaiNamrin   = t5 == null ? 0.00 : (double)t5.energy,
                               phaJome      = t6 == null ? 0.00 : (double)t6.energy,
                               phom         = t7 == null ? 0.00 : (double)t7.energy,
                               soonChar     = t8 == null ? 0.00 : (double)t8.energy,
                           };
                List<PowerViewModel> newList = new List<PowerViewModel>();
                PowerViewModel sumEnergy = new PowerViewModel();
                newList = list.ToList();
                for (int i=23; i>=0; i--)
                {
                    if (i == 0)
                    {
                        newList[i].doiNgam = 0.00;
                        newList[i].rawWater = 0.00;
                        newList[i].high1 = 0.00;
                        newList[i].high2 = 0.00;
                        newList[i].huaiNamrin = 0.00;
                        newList[i].phaJome = 0.00;
                        newList[i].phom = 0.00;
                        newList[i].soonChar = 0.00;
                    }
                    else
                    {
                        newList[i].doiNgam = (newList[i - 1].doiNgam == 0.00) ? 0.00 : (newList[i].doiNgam > newList[i - 1].doiNgam) ? Math.Round(newList[i].doiNgam - newList[i - 1].doiNgam, 2) : 0.00;
                        newList[i].rawWater = (newList[i - 1].rawWater == 0.00) ? 0.00 : (newList[i].rawWater > newList[i - 1].rawWater) ? Math.Round(newList[i].rawWater - newList[i - 1].rawWater, 2) : 0.00;
                        newList[i].high1 = (newList[i - 1].high1 == 0.00) ? 0.00 : (newList[i].high1 > newList[i - 1].high1) ? Math.Round(newList[i].high1 - newList[i - 1].high1, 2) : 0.00;
                        newList[i].high2 = (newList[i - 1].high2 == 0.00) ? 0.00 : (newList[i].high2 > newList[i - 1].high2) ? Math.Round(newList[i].high2 - newList[i - 1].high2, 2) : 0.00;
                        newList[i].huaiNamrin = (newList[i - 1].huaiNamrin == 0.00) ? 0.00 : (newList[i].huaiNamrin > newList[i - 1].huaiNamrin) ? Math.Round(newList[i].huaiNamrin - newList[i - 1].huaiNamrin, 2) : 0.00;
                        newList[i].phaJome = (newList[i - 1].phaJome == 0.00) ? 0.00 : (newList[i].phaJome > newList[i - 1].phaJome) ? Math.Round(newList[i].phaJome - newList[i - 1].phaJome, 2) : 0.00;
                        newList[i].phom = (newList[i - 1].phom == 0.00) ? 0.00 : (newList[i].phom > newList[i - 1].phom) ? Math.Round(newList[i].phom - newList[i - 1].phom, 2) : 0.00;
                        newList[i].soonChar = (newList[i - 1].soonChar == 0.00) ? 0.00 : (newList[i].soonChar > newList[i - 1].soonChar) ? Math.Round(newList[i].soonChar - newList[i - 1].soonChar, 2) : 0.00;
                    }
                    sumEnergy.timeStamp = "Total";
                    sumEnergy.doiNgam = Math.Round(sumEnergy.doiNgam + newList[i].doiNgam, 2);
                    sumEnergy.rawWater = Math.Round(sumEnergy.rawWater + newList[i].rawWater, 2);
                    sumEnergy.high1 = Math.Round(sumEnergy.high1 + newList[i].high1, 2);
                    sumEnergy.high2 = Math.Round(sumEnergy.high2 + newList[i].high2, 2);
                    sumEnergy.huaiNamrin = Math.Round(sumEnergy.huaiNamrin + newList[i].huaiNamrin, 2);
                    sumEnergy.phaJome = Math.Round(sumEnergy.phaJome + newList[i].phaJome, 2);
                    sumEnergy.phom = Math.Round(sumEnergy.phom + newList[i].phom, 2);
                    sumEnergy.soonChar = Math.Round(sumEnergy.soonChar + newList[i].soonChar, 2);
                }
                newList.Add(sumEnergy);
                return newList;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return null;
            }
        }