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

问题描述 投票:0回答:2

这是我的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。这包含所有员工出勤日期明智。这将是一个很大的名单。我需要将每组记录发送到验证方法和empid。我是通过上面的代码实现的。但它花了很多时间。有没有更好的方法来跳过所有的AttendancesPerDay记录,而不是像循环中一个一个地去。

注意:在一天内会有多个条目,比如每次emp出去并进来。比方说20条记录。我希望所有20条记录都能将其发送给验证。并且下一次迭代必须是21条记录。但现在我只是继续这一点

c# .net loops
2个回答
2
投票

你可以在linq中使用qazxsw poi方法并为你的IEnumerable<TSource>.Except类实现qazxsw poi。

IEqualityComparer<EmployeeAttendance>

并将你的逻辑改为此

EmployeeAttendance

0
投票

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

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;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.