I have an Employee
class which defined as this:
Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool isOff { get; set; }
}
This is my class implementation and usage:
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},
};
In the initialization above there is only 1 record that is off:
Id = 1, Name = "Emp 1" and the WorkDate = 4/13/2016
Now how can i get the id of an employee which has no day off throughout the week (from April 11-17)? Which is Id = 2
A LINQ
solution is far better but i dont know how to do it.
UPDATED
To avoid confusion of the object Employee
i changed it to EmployeeSchedule
class EmployeeSchedule
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool isOff { get; set; }
}
This is the implementation
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},
};
The selected answer is still applicable.
Now how can i get the id of an employee which has no day off throughout the week (from April 11-17)? Which is Id = 2
You could use Linq
extensions, and achieve this.
var empid = workers.GroupBy(g=> g.Id)
.Where(x=>x.All(e=>!e.IsOff))
.Select(x=>x.Key)
That means you need a fromDate
, toDate
, and you have to find the employees from the list subject to the condition that, WorkDate
should be in between fromDate
and toDate
, and also isOff == false
that means has no day off throughout the week
So you can use the following snippet :
DateTime fromDate = new DateTime(2016, 04, 11);
DateTime toDate = new DateTime(2016, 04, 17);
var noDayOfList = workers.GroupBy(x=> x.Id)
.Where(x =>
x.All(y=>
y.WorkDate >= fromDate &&
y.WorkDate <= toDate &&
!y.isOff))
.Select(z=>z.Key).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With