获取表和汇总天数中的上一个列表

时间:2018-11-15 12:28:33

标签: c# asp.net-mvc entity-framework

获取上一年的清单

我正在创建一个休假管理系统。我必须从表格中获取雇员的前几年的清单,并汇总天数(如果有的话)。如果没有往年,那么我必须选择当年的日子。 FindEmployee方法返回在休假表中具有其数据和日期的员工列表。

这是请假模式

public class LeaveTable
{
    public Employee Employee { get; set; }
    public string EmployeeId { get; set; }
    public Leave Leave { get; set; }
    public string LeaveCode { get; set; }
    public int Days { get; set; }
    public int Balance { get; set; }
    public int UsedDays { get; set; }
    public int Year { get; set; }
}

这是我到目前为止尝试过的

public async Task<decimal> CheckEmployeeOutstandingBalance(string employeeId)
        {
            var employees = FindEmployee(employeeId).Result.ToList();
            var currentYear = DateTime.Now.Year;
            var previousYear = currentYear - 1;
            var days = 0;

            if(previousYear != 0)
             days = employees.Sum(s => s.Balance);



            foreach (var employee in employees)
            {

                if (previousYear != 0)
                {
                    days = employee
                }
            }



            //return await Task.FromResult(employees);
        }

任何建议获取上一年的清单并汇总天数(如果有的话)。

2 个答案:

答案 0 :(得分:0)

 if(previousYear != 0)
       days = employees.Sum(s => s.Balance);
  else
    {
     days = LeaveTable.Where(l => l.Year <currentYear && l.EmployeeId == employeeId ).Sum( li => li.Balance)
    }

答案 1 :(得分:0)

我编辑了代码,此方法有效。

  public async Task<decimal> CheckEmployeeOutstandingBalance(string employeeId)
            {
                var employee = FindEmployee(employeeId).Result.ToList();//returns list of employees
                var currentYear = DateTime.Now.Year;
                var balanceSum = 0;
                var results = 0;
                var currentBalance = 0;
                var prev = employee.Where(s => s.Year != currentYear).ToList();
                var currentyear = employee.Where(s => s.Year == currentYear).FirstOrDefault();
                 currentBalance = currentyear.Balance;
                balanceSum = prev.Sum(s => s.Balance);

                 results = balanceSum + currentBalance;
                return await Task.FromResult(results);
            }