从foreach循环返回所有值

时间:2019-06-26 12:32:37

标签: c#

请在foreach循环中有一个for循环,在for循环中,每个雇员的工资单中都有一个计算,当我返回输出时,它仅返回第一个,请问如何返回所有员工一次?

这是我的下面的代码

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);
            }
        }

上面的代码仅针对第一位员工返回,如果我将return语句放在循环外,我不知道如何获取内部信息,那么有没有办法可以为每位员工执行工资计算,那么一次返回所有输出?

2 个答案:

答案 0 :(得分:1)

在代码顶部添加一个空列表,并在for循环中填充要返回的值:

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;

答案 1 :(得分:0)

我假设您是编程的新手,我对代码进行了更改,可以在其中编写收益回报。

我已经注释了一个代码,您可以在其中再次查询计数,但是该查询与之前的代码类似,您可以查看是否正在循环,这是我在做什么。

我不建议这样的代码包含很多if..else,但味道很差,但这是您可以弄清楚的。

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);
            }
        }