/* * * sort equal sort by values : * 1 -> sort by load * 2-> sort by type * * * if sortValue equal 1 val will be value of load * * if sortValue equal 2 val will be value of type * type: 1-> current * type:2->voltage * type:3->powerFactor * */ public List <querySource> AveragePerDayfromajax(int id, int sort, string val, DateTime date) { FactoryService fs = new FactoryService(_Context); ViewBag.factories = fs.GetAllFactoriesSimple(); ViewBag.factoryname = fs.GetFactoryNameById(id); ViewBag.date = date.ToString("yyyy/MM/dd hh"); ReportService r = new ReportService(_Context); List <querySource> res = new List <querySource>(); int i = 0; if (sort == 1) { var load = _Context.Load.FirstOrDefault(l => l.Id == Convert.ToInt32(val)); SourceReading src; src = _Context.SourceReading.LastOrDefault(s => s.PrimarySourceId == load.SourceId && s.TimeStamp.Day == date.Day && s.Fac_Id == id); if (src != null) { res.Add(new querySource()); r.GetAveragePerDay(id, date, ref res[i].CurrentAvg, ref res[i].HarmAvg, ref res[i].VoltageAvg, ref res[i].PowerFactorAvg, load.SourceId); res[i].name = load.name; res[i].sourceid = load.SourceId; res[i].id = load.Id; res[i].facid = id; res[i].Timestamp = date; } } else { var loads = _Context.Load.Where(l => l.Function == val).ToList(); foreach (var x in loads) { SourceReading src; if (x.SourceId % 2 == 0) { // src = _Context.SourceReading.Last(s => s.SecondarySourceId == x.SourceId && s.TimeStamp.Day == date.Day && s.Fac_Id == id); src = _Context.SourceReading.LastOrDefault(s => s.SecondarySourceId == x.SourceId && s.TimeStamp.Day == date.Day && s.Fac_Id == id); } else { src = _Context.SourceReading.LastOrDefault(s => s.PrimarySourceId == x.SourceId && s.TimeStamp.Day == date.Day && s.Fac_Id == id); } if (src != null) { res.Add(new querySource()); r.GetAveragePerDay(id, date, ref res[i].CurrentAvg, ref res[i].HarmAvg, ref res[i].VoltageAvg, ref res[i].PowerFactorAvg, x.SourceId); res[i].name = x.name; res[i].sourceid = x.SourceId; res[i].id = x.Id; res[i].facid = id; res[i].Timestamp = date; i++; } else { continue; } } } return(res); }