public List <TotalRegionAdmissions> GetTotalAdmittedKidsPerDistrict(int Facility_Id) { int Province_Id = (from f in db.apl_Cyca_Facility join p in db.Provinces on f.Province_Id equals p.Province_Id where f.Facility_Id == Facility_Id select p.Province_Id).SingleOrDefault(); var StartDate = new DateTime(2020, 04, 01); var EndDate = new DateTime(2020, 06, 30); var models = db.CYCA_TotalFemalesAdmittedPerDistrict1(Province_Id, StartDate, EndDate).ToList(); var Malesmodels = db.CYCA_TotalMalesAdmittedPerDistrict1(Province_Id, StartDate, EndDate).ToList(); var viewModels = new List <TotalRegionAdmissions>(); var monthone = 0; var monthtwo = 0; var monththree = 0; var monthoneMale = 0; var monthtwoMale = 0; var monththreeMale = 0; foreach (var m in models) { var splitDate = Convert.ToDateTime(m.MONTH).Month; var Male = new CYCA_TotalMalesAdmittedPerDistrict1_Result(); var reg = new TotalRegionAdmissions { Province_Id = m.Province_Id, Region_Id = m.District_Id, Gender = m.Gender, RegionName = m.RegionName }; var males = Malesmodels.Where(a => a.District_Id == m.District_Id).ToList(); foreach (var item in males) { var split = Convert.ToDateTime(item.MONTH).Month; if (split == splitDate) { Male = item; } } switch (splitDate) { case 1: monthone = Convert.ToInt32(m.Total); monthoneMale = Convert.ToInt32(Male.Total); //reg.MonthOne.MonthName = "Jan"; break; case 2: monthtwo = Convert.ToInt32(m.Total); //reg.MonthTwo.MonthName = "Feb"; break; case 3: monththree = Convert.ToInt32(m.Total); //reg.MonthThree.MonthName = "Mar"; break; case 4: monthone = Convert.ToInt32(m.Total); monthoneMale = Convert.ToInt32(Male.Total); //reg.MonthOne.MonthName = "Apr"; break; case 5: monthtwo = Convert.ToInt32(m.Total); monthtwoMale = Convert.ToInt32(Male.Total); //reg.MonthTwo.MonthName = "May"; break; case 6: monththree = Convert.ToInt32(m.Total); monththreeMale = Convert.ToInt32(Male.Total); //reg.MonthThree.MonthName = "Jun"; break; case 7: monthone = Convert.ToInt32(m.Total); //reg.MonthOne.MonthName = "Jul"; break; case 8: monthtwo = Convert.ToInt32(m.Total); //reg.MonthTwo.MonthName = "Aug"; break; case 9: monththree = Convert.ToInt32(m.Total); //reg.MonthThree.MonthName = "Sep"; break; case 10: monthone = Convert.ToInt32(m.Total); //reg.MonthOne.MonthName = "Oct"; break; case 11: monthtwo = Convert.ToInt32(m.Total); //reg.MonthTwo.MonthName = "Nov"; break; case 12: monththree = Convert.ToInt32(m.Total); //reg.MonthThree.MonthName = "Dec"; break; default: break; } var first = new MonthDetails() { MonthName = "Jan", TotalFemales = monthone, TotalMales = monthoneMale }; var second = new MonthDetails() { MonthName = "Feb", TotalFemales = monthtwo, TotalMales = monthtwoMale }; var third = new MonthDetails() { MonthName = "Mar", TotalFemales = monththree, TotalMales = monththreeMale }; var disctrictExists = false; if (viewModels.Count > 0) { foreach (var mod in viewModels) { if (mod.Region_Id == reg.Region_Id) { var model = viewModels.Where(v => v.Region_Id == reg.Region_Id).FirstOrDefault(); //var males = Malesmodels.Where(a => a.District_Id == m.District_Id).FirstOrDefault(); int index = viewModels.IndexOf(model); disctrictExists = true; if (first.TotalFemales != 0) { model.MonthOne.TotalFemales = first.TotalFemales; } if (second.TotalFemales != 0) { model.MonthTwo.TotalFemales = second.TotalFemales; } if (third.TotalFemales != 0) { model.MonthThree.TotalFemales = third.TotalFemales; } if (first.TotalMales != 0) { model.MonthOne.TotalMales = first.TotalMales; } if (second.TotalMales != 0) { model.MonthTwo.TotalMales = second.TotalMales; } if (third.TotalMales != 0) { model.MonthThree.TotalMales = third.TotalMales; } viewModels[index] = model; break; } else { //monthone = 0; //monthtwo = 0; //monththree = 0; disctrictExists = false; } } } else { disctrictExists = true; reg.MonthOne = new MonthDetails() { MonthName = "Jan", TotalFemales = monthone, TotalMales = monthoneMale }; reg.MonthTwo = new MonthDetails() { MonthName = "Feb", TotalFemales = monthtwo, TotalMales = monthtwoMale }; reg.MonthThree = new MonthDetails() { MonthName = "Mar", TotalFemales = monththree, TotalMales = monththreeMale }; viewModels.Add(reg); } if (disctrictExists) { } else { reg.MonthOne = new MonthDetails() { MonthName = "Jan", TotalFemales = monthone, TotalMales = monthoneMale }; reg.MonthTwo = new MonthDetails() { MonthName = "Feb", TotalFemales = monthtwo, TotalMales = monthtwoMale }; reg.MonthThree = new MonthDetails() { MonthName = "Mar", TotalFemales = monththree, TotalMales = monththreeMale }; viewModels.Add(reg); } //foreach (var i in Malesmodels) //{ // totMale = i.Total; //} //viewModels.Add(reg); } return(viewModels); }