вы можете использовать IEnumerable<TSource>.Except
метод в linq и импелмент 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);
}