Mam Employee
klasy, które zdefiniowane jako to:Zaznacz rekordy, które nie ma dnia wolnego w ciągu tygodnia w List <T> - C#
Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool isOff { get; set; }
}
To moja klasa wdrożenie i użytkowanie:
List<Employee> workers = new List<Employee>()
{
new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/13/2016"), IsOff = true},
new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/14/2016"), IsOff = false},
new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/15/2016"), IsOff = false},
new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/16/2016"), IsOff = false},
new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/17/2016"), IsOff = false},
new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/13/2016"), IsOff = false},
new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/14/2016"), IsOff = false},
new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/15/2016"), IsOff = false},
new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/16/2016"), IsOff = false},
new Employee { Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/17/2016"), IsOff = false},
};
W inicjacji powyżej jest tylko 1 rekord, który jest wyłączony:
Id = 1, Name = "Emp 1" and the WorkDate = 4/13/2016
Jak mogę uzyskać identyfikator pracownika, który nie ma wolnego dnia przez cały tydzień (od 11 do 17 kwietnia)? To jest rozwiązanie znacznie lepsze, ale nie wiem jak to zrobić.
AKTUALIZACJA
Aby uniknąć nieporozumień obiektu Employee
Zmieniłem go EmployeeSchedule
class EmployeeSchedule
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool isOff { get; set; }
}
To jest realizacja
List<EmployeeSchedule> workers = new List<EmployeeSchedule>()
{
new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/13/2016"), IsOff = true},
new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/14/2016"), IsOff = false},
new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/15/2016"), IsOff = false},
new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/16/2016"), IsOff = false},
new EmployeeSchedule{ Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/17/2016"), IsOff = false},
new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/13/2016"), IsOff = false},
new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/14/2016"), IsOff = false},
new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/15/2016"), IsOff = false},
new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/16/2016"), IsOff = false},
new EmployeeSchedule{ Id = 2, Name = "Emp 2", WorkDate = Convert.ToDateTime("4/17/2016"), IsOff = false},
};
Wybrana odpowiedź jest nadal stosowane.
Czy wyrażenie Lambda ok? – Bharat
'var list = workers.Where (x => x.IsOff == false)' nie wiem, jakie rozszerzenie musi zostać wywołane do filtrowania 'Id = 2' –
@Bharat Potrzebuję' LINQ' sir. –