问题
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.
回答1:
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)
回答2:
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();
回答3:
You can write something like this:
var startDate = new DateTime(2016,04,11);
var endDate = new DateTime(2016,04,17);
var ids = workers
.Where(w => w.WorkDate >= startDate)
.Where(w => w.WorkDate <= endDate)
.GroupBy(w => w.Id)
.Where(g => !g.Any(w => w.IsOff))
.Select(g => g.Key);
Essentially:
- Filter the rows you need (in this time frame)
- Group by employee id
- Make sure none of the rows in the group are marked
IsOff
- Select the grouping key (in this case, it'll be the Employee's Id)
Note though, it's a bit confusing calling the object Employee
, as it really represents an employee's schedule, rather than an employee itself
回答4:
try, this you have two condition id and date but your id is sufficient for get your requirement,
List<string> resEmp = new List<string>();
foreach (var item in workers )
{
if (item.Id == 2 and item.IsOff == false)
{
resEmp.Add(item);
}
}
回答5:
Look at these code too.
i have defined a Model class of Employee to get the data,
maybe currently you need Id but in Future you can use another properties too.
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool IsOff { get; set; }
}
and Code wtih Linq Expression,
Employee objEmployee = new Employee();
objEmployee.Id = workers
.GroupBy(itm => itm.Id)
.Where(itm => !itm.Any(m => m.IsOff))
.Select(itm => itm.Key)
.FirstOrDefault();
来源:https://stackoverflow.com/questions/36565288/select-records-which-has-no-day-off-throughout-the-week-in-listt-c-sharp