如何将记录列表传递给方法并跳过所有传递的记录

时间:2019-04-15 04:53:02

标签: c# .net loops

这是我的Attendance

public class EmployeeAttendance
{
    public string Employee { get; set; }
    public DateTime Date { get; set; }
    public string EmployeeClockTimeId { get; set; }
}

逻辑

        List<EmployeeAttendance> attendancesPerDay = new List<EmployeeAttendance>();
        List<EmployeeAttendance> employeeAttendances = new List<EmployeeAttendance>();
        foreach (var attendance in employeeAttendances)
        {
            if (attendancesPerDay.Contains(attendance))
            {
                continue;
            }
            attendancesPerDay = employeeAttendances.Where(x => x.Date == DateTime.Now.Date && x.EmployeeClockTimeId == 12.ToString()).ToList();
            Validation(attendancesPerDay);
        }

我将拥有employeeAttendances。其中包含所有员工的出勤日期。这将是一个很大的清单。我需要每天(每天)将这些记录集发送到验证方法,并且要保持空率。我是通过上面的代码实现的。但是要花很多时间是否有更好的方法来执行此操作,例如跳过所有AttendancesPerDay记录,而不是像一个循环地一步一步走。

注意:有一天,就像emp每次出入一样,都会有多个条目。可以说20条记录。我希望所有20条记录都将其发送给验证。下一次迭代必须是21条记录。但现在我要继续到这一点

2 个答案:

答案 0 :(得分:2)

您可以在linq中使用IEnumerable<TSource>.Except方法并为IEqualityComparer<EmployeeAttendance>类使用元素EmployeeAttendance

public class EmployeeAttendanceEqualityComparer : IEqualityComparer<EmployeeAttendance>
    {
        public bool Equals(EmployeeAttendance x, EmployeeAttendance y)
        {
            if (x == null || y == null)
                return false;

            // check your equality same as this
            return x.Employee == y.Employee;
        }

        public int GetHashCode(EmployeeAttendance obj)
        {
            // or something else
            return 12;
        }
    }

并更改您的逻辑

var currentTime = DateTime.Now;

var attendancesPerDay = new List<EmployeeAttendance>
{
    new EmployeeAttendance { Date = currentTime, Employee = "1", EmployeeClockTimeId = "11" },
    new EmployeeAttendance { Date = currentTime, Employee = "2", EmployeeClockTimeId = "12" },
    new EmployeeAttendance { Date = currentTime, Employee = "3", EmployeeClockTimeId = "13" },
    new EmployeeAttendance { Date = currentTime, Employee = "4", EmployeeClockTimeId = "14" },
    new EmployeeAttendance { Date = currentTime, Employee = "5", EmployeeClockTimeId = "15" },
};

var employeeAttendances = new List<EmployeeAttendance>
{
    new EmployeeAttendance { Date = currentTime, Employee = "1", EmployeeClockTimeId = "11" },
    new EmployeeAttendance { Date = currentTime, Employee = "2", EmployeeClockTimeId = "12" },
    new EmployeeAttendance { Date = currentTime, Employee = "3", EmployeeClockTimeId = "13" },
};

var result = attendancesPerDay
    .Except(employeeAttendances, new EmployeeAttendanceEqualityComparer())
    .Where(x => x.Date == DateTime.Now.Date && x.EmployeeClockTimeId == "12").ToList();

foreach (EmployeeAttendance employeeAttendance in result)
{
    Validation(employeeAttendance);
}

答案 1 :(得分:0)

我想太多了。这就是为什么我错过了这个简单的事情。我最终使用了这样的

        for (int i = 0; i < EmployeeAttandance.Count; i++)
        {
            var attendancesPerDay = EmployeeAttandance.Where(x => x.Date.Date == EmployeeAttandance[i].Date.Date && x.EmployeeClockTimeID == EmployeeAttandance[i].EmployeeClockTimeID).ToList();
            Validation(attendancesPerDay);
            i = EmployeeAttandance.IndexOf(attendancesPerDay[attendancesPerDay.Count - 1]);
        }