0

Please I have a for loop in a foreach loop, and in the for loop, there's a calculation in it for each employee payroll, when I return the output, it returns for only the first one, please how do I return for all employees at once?

Here is my code below

var templateId = (from o in db.PayrollTemplate
                          select new
                          {
                              o.TemplateId,
                              o.GradeId
                          }).ToList();
        foreach (var items in templateId)
        {
            var empToRun = (from x in db.Employee
                            join y in db.SalaryInfo on x.Id equals y.EmployeeId
                            where x.GradeLevel == items.GradeId && x.Id == y.EmployeeId
                            select new
                            {
                                Id = x.Id
                            }).ToList();
            var empToCount = (from x in db.Employee
                              join y in db.SalaryInfo on x.Id equals y.EmployeeId
                              where x.GradeLevel == items.GradeId && x.Id == y.EmployeeId
                              select x).Count();
            for (int i = -1; i <= empToCount;)
            {
                i++;
                decimal payE = 0;
                var empId = empToRun[i].Id;
                var getGross = (from o in db.Employee
                                join a in db.SalaryInfo on o.Id equals a.EmployeeId
                                where o.Id == empId
                                select new
                                {
                                    GrossPay = a.GrossPay
                                }).FirstOrDefault();



                var percent = 20 * getGross.GrossPay;
                var twentyPercent = percent / 100;
                var cra = 200000 + twentyPercent;
                var taxAbleIncome = getGross.GrossPay - cra;

                if (taxAbleIncome >= 300000)
                {
                    var sevenPercent = (7 * 300000) / 100;
                    payE = sevenPercent;
                    taxAbleIncome = taxAbleIncome - 300000;
                }
                else
                {
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    return Json(Yearly + " " + Monthly);
                }
                if (taxAbleIncome >= 300000)
                {
                    var elevenPercent = (11 * 300000) / 100;
                    payE = payE + elevenPercent;
                    taxAbleIncome = taxAbleIncome - 300000;
                }
                else
                {
                    var elevenPercent = (11 * taxAbleIncome) / 100;
                    payE = payE + (decimal)elevenPercent;
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    return Json(Yearly + " " + Monthly);
                }
                if (taxAbleIncome >= 500000)
                {
                    var fifteenPercent = (15 * 500000) / 100;
                    payE = payE + fifteenPercent;
                    taxAbleIncome = taxAbleIncome - 500000;
                }
                else
                {
                    var fifteenPercent = (15 * taxAbleIncome) / 100;
                    payE = payE + (decimal)fifteenPercent;
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    return Json(Yearly + " " + Monthly);
                }
                if (taxAbleIncome >= 500000)
                {
                    var nineteenPercent = (19 * 500000) / 100;
                    payE = payE + nineteenPercent;
                    taxAbleIncome = taxAbleIncome - 500000;
                }
                else
                {
                    var nineteenPercent = (19 * taxAbleIncome) / 100;
                    payE = payE + (decimal)nineteenPercent;
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    return Json(Yearly + " " + Monthly);
                }
                if (taxAbleIncome > 1600000)
                {
                    var twentyonePercent = (21 * 1600000) / 100;
                    payE = payE + twentyonePercent;
                    taxAbleIncome = taxAbleIncome - 1600000;
                }
                else
                {
                    var twentyonePercent = (21 * 1600000) / 100;
                    payE = payE + twentyonePercent;
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    return Json(Yearly + " " + Monthly);
                }
                if (taxAbleIncome > 3200000)
                {
                    var twentyfourPercent = (24 * 3200000) / 100;
                    payE = payE + twentyfourPercent;
                }
                else
                {
                    var twentyfourPercent = (21 * taxAbleIncome) / 100;
                    payE = payE + (decimal)twentyfourPercent;
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    return Json(Yearly + " " + Monthly);
                }

                var Year = "Year PayE: " + payE;
                var Month = "Monthly PayE: " + payE / 12;
                return Json(Year + " " + Month);
            }
        }

The above code returns just for the first employee, and if I put the return statement outside the loop I don't know how to get what's inside, So is there a way I can perform the payroll calculation for each employee, then return all output at once?

2 Answers2

1

Add an empty list at the top of your code and fill it with the values that you want to return in the for loop:

var templateId = (from o in db.PayrollTemplate
                      select new
                      {
                          o.TemplateId,
                          o.GradeId
                      }).ToList();

    List<JsonResult> list = new List<JsonResult>();
    foreach (var items in templateId)
    {
        var empToRun = (from x in db.Employee
                        join y in db.SalaryInfo on x.Id equals y.EmployeeId
                        where x.GradeLevel == items.GradeId && x.Id == y.EmployeeId
                        select new
                        {
                            Id = x.Id
                        }).ToList();
        var empToCount = (from x in db.Employee
                          join y in db.SalaryInfo on x.Id equals y.EmployeeId
                          where x.GradeLevel == items.GradeId && x.Id == y.EmployeeId
                          select x).Count();

        for (int i = 0; i <= empToCount;i++)
        {
            decimal payE = 0;
            var empId = empToRun[i].Id;
            var getGross = (from o in db.Employee
                            join a in db.SalaryInfo on o.Id equals a.EmployeeId
                            where o.Id == empId
                            select new
                            {
                                GrossPay = a.GrossPay
                            }).FirstOrDefault();



            var percent = 20 * getGross.GrossPay;
            var twentyPercent = percent / 100;
            var cra = 200000 + twentyPercent;
            var taxAbleIncome = getGross.GrossPay - cra;

            if (taxAbleIncome >= 300000)
            {
                var sevenPercent = (7 * 300000) / 100;
                payE = sevenPercent;
                taxAbleIncome = taxAbleIncome - 300000;
            }
            else
            {
                var Yearly = "Year PayE: " + payE;
                var pmonth = payE / 12;
                var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                var ret = Json(Yearly + " " + Monthly);
                list.add(ret);
                continue;
            }
            if (taxAbleIncome >= 300000)
            {
                var elevenPercent = (11 * 300000) / 100;
                payE = payE + elevenPercent;
                taxAbleIncome = taxAbleIncome - 300000;
            }
            else
            {
                var elevenPercent = (11 * taxAbleIncome) / 100;
                payE = payE + (decimal)elevenPercent;
                var Yearly = "Year PayE: " + payE;
                var pmonth = payE / 12;
                var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                var ret = Json(Yearly + " " + Monthly);
                list.add(ret);
                continue;
            }
            if (taxAbleIncome >= 500000)
            {
                var fifteenPercent = (15 * 500000) / 100;
                payE = payE + fifteenPercent;
                taxAbleIncome = taxAbleIncome - 500000;
            }
            else
            {
                var fifteenPercent = (15 * taxAbleIncome) / 100;
                payE = payE + (decimal)fifteenPercent;
                var Yearly = "Year PayE: " + payE;
                var pmonth = payE / 12;
                var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                var ret = Json(Yearly + " " + Monthly);
                list.add(ret);
                continue;
            }
            if (taxAbleIncome >= 500000)
            {
                var nineteenPercent = (19 * 500000) / 100;
                payE = payE + nineteenPercent;
                taxAbleIncome = taxAbleIncome - 500000;
            }
            else
            {
                var nineteenPercent = (19 * taxAbleIncome) / 100;
                payE = payE + (decimal)nineteenPercent;
                var Yearly = "Year PayE: " + payE;
                var pmonth = payE / 12;
                var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                var ret = Json(Yearly + " " + Monthly);
                list.add(ret);
                continue;
            }
            if (taxAbleIncome > 1600000)
            {
                var twentyonePercent = (21 * 1600000) / 100;
                payE = payE + twentyonePercent;
                taxAbleIncome = taxAbleIncome - 1600000;
            }
            else
            {
                var twentyonePercent = (21 * 1600000) / 100;
                payE = payE + twentyonePercent;
                var Yearly = "Year PayE: " + payE;
                var pmonth = payE / 12;
                var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                var ret = Json(Yearly + " " + Monthly);
                list.add(ret);
                continue;
            }
            if (taxAbleIncome > 3200000)
            {
                var twentyfourPercent = (24 * 3200000) / 100;
                payE = payE + twentyfourPercent;
            }
            else
            {
                var twentyfourPercent = (21 * taxAbleIncome) / 100;
                payE = payE + (decimal)twentyfourPercent;
                var Yearly = "Year PayE: " + payE;
                var pmonth = payE / 12;
                var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                var ret = Json(Yearly + " " + Monthly);
                list.add(ret);
                continue;
            }

            var Year = "Year PayE: " + payE;
            var Month = "Monthly PayE: " + payE / 12;
            var ret = Json(Year + " " + Month);
            list.add(ret);
        }
    }
    return list;
Elias N
  • 1,430
  • 11
  • 19
0

I am assuming you are fairly new to programming, I have made the changes to code where you can write yield return.

I have commented one code where you query again for count, however, the query is similar to earlier one and you can see if for loop, what I am doing.

I am not going to advice that this code with so many if..else, smells bad, but, this is something you can figure out.

var templateId = (from o in db.PayrollTemplate
                          select new
                          {
                              o.TemplateId,
                              o.GradeId
                          }).ToList();
        foreach (var items in templateId)
        {
            var empToRun = (from x in db.Employee
                            join y in db.SalaryInfo on x.Id equals y.EmployeeId
                            where x.GradeLevel == items.GradeId && x.Id == y.EmployeeId
                            select new
                            {
                                Id = x.Id
                            }).ToList();
            //var empToCount = (from x in db.Employee
            //                  join y in db.SalaryInfo on x.Id equals y.EmployeeId
            //                  where x.GradeLevel == items.GradeId && x.Id == y.EmployeeId
            //                  select x).Count();
            for (int i = -1; i <= empToRun.Count();)
            {
                i++;
                decimal payE = 0;
                var empId = empToRun[i].Id;
                var getGross = (from o in db.Employee
                                join a in db.SalaryInfo on o.Id equals a.EmployeeId
                                where o.Id == empId
                                select new
                                {
                                    GrossPay = a.GrossPay
                                }).FirstOrDefault();



                var percent = 20 * getGross.GrossPay;
                var twentyPercent = percent / 100;
                var cra = 200000 + twentyPercent;
                var taxAbleIncome = getGross.GrossPay - cra;

                if (taxAbleIncome >= 300000)
                {
                    var sevenPercent = (7 * 300000) / 100;
                    payE = sevenPercent;
                    taxAbleIncome = taxAbleIncome - 300000;
                }
                else
                {
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    yield return Json(Yearly + " " + Monthly);
                }
                if (taxAbleIncome >= 300000)
                {
                    var elevenPercent = (11 * 300000) / 100;
                    payE = payE + elevenPercent;
                    taxAbleIncome = taxAbleIncome - 300000;
                }
                else
                {
                    var elevenPercent = (11 * taxAbleIncome) / 100;
                    payE = payE + (decimal)elevenPercent;
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    yield return Json(Yearly + " " + Monthly);
                }
                if (taxAbleIncome >= 500000)
                {
                    var fifteenPercent = (15 * 500000) / 100;
                    payE = payE + fifteenPercent;
                    taxAbleIncome = taxAbleIncome - 500000;
                }
                else
                {
                    var fifteenPercent = (15 * taxAbleIncome) / 100;
                    payE = payE + (decimal)fifteenPercent;
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    yield return Json(Yearly + " " + Monthly);
                }
                if (taxAbleIncome >= 500000)
                {
                    var nineteenPercent = (19 * 500000) / 100;
                    payE = payE + nineteenPercent;
                    taxAbleIncome = taxAbleIncome - 500000;
                }
                else
                {
                    var nineteenPercent = (19 * taxAbleIncome) / 100;
                    payE = payE + (decimal)nineteenPercent;
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    yield return Json(Yearly + " " + Monthly);
                }
                if (taxAbleIncome > 1600000)
                {
                    var twentyonePercent = (21 * 1600000) / 100;
                    payE = payE + twentyonePercent;
                    taxAbleIncome = taxAbleIncome - 1600000;
                }
                else
                {
                    var twentyonePercent = (21 * 1600000) / 100;
                    payE = payE + twentyonePercent;
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    yield return Json(Yearly + " " + Monthly);
                }
                if (taxAbleIncome > 3200000)
                {
                    var twentyfourPercent = (24 * 3200000) / 100;
                    payE = payE + twentyfourPercent;
                }
                else
                {
                    var twentyfourPercent = (21 * taxAbleIncome) / 100;
                    payE = payE + (decimal)twentyfourPercent;
                    var Yearly = "Year PayE: " + payE;
                    var pmonth = payE / 12;
                    var Monthly = "Monthly PayE: " + Math.Round(pmonth, 2);
                    yield return Json(Yearly + " " + Monthly);
                }

                var Year = "Year PayE: " + payE;
                var Month = "Monthly PayE: " + payE / 12;
                yield return Json(Year + " " + Month);
            }
        }
r2018
  • 505
  • 4
  • 14
  • I've got this error from this solution "Message": "An error has occurred.", "ExceptionMessage": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.", "ExceptionType": "System.InvalidOperationException", "StackTrace": null, "InnerException": { "Message": "An error has occurred.", "ExceptionMessage": "Error getting value from 'ReadTimeout' on 'System.Web.HttpInputStream'.", "ExceptionType": "Newtonsoft.Json.JsonSerializationException", – Emmanuel Ikechukwu Jun 26 '19 at 13:15
  • Is this code part of Controller Action? If yes, I would suggest create a List outside loop, and then add the messages there, and then later return as JSON. – r2018 Jun 26 '19 at 13:30