Select 列表中一周没有休息日的记录<T> - C#
Select records which has no day-off throughout the week in List<T> - C#
我有一个 Employee
class 定义如下:
Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool isOff { get; set; }
}
这是我的 class 实现和用法:
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},
};
在上面的初始化中只有1条记录是关闭的:
Id = 1, Name = "Emp 1" and the WorkDate = 4/13/2016
现在如何获取整个星期(4 月 11 日至 17 日)没有休息日的员工的 ID?即 Id = 2
LINQ
解决方案要好得多,但我不知道该怎么做。
UPDATED
为了避免混淆对象 Employee
我将其更改为 EmployeeSchedule
class EmployeeSchedule
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool isOff { get; set; }
}
这是实现
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},
};
所选答案仍然适用。
试试,你有两个条件 ID 和日期,但你的 ID 足以满足你的要求,
List<string> resEmp = new List<string>();
foreach (var item in workers )
{
if (item.Id == 2 and item.IsOff == false)
{
resEmp.Add(item);
}
}
这意味着你需要 fromDate
、toDate
,你必须从列表中找到员工,条件是 WorkDate
应该在 [=11] 之间=] 和 toDate
,还有 isOff == false
意思是 has no day off throughout the week
所以你可以使用下面的代码片段:
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();
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
您可以使用 Linq
扩展来实现这一点。
var empid = workers.GroupBy(g=> g.Id)
.Where(x=>x.All(e=>!e.IsOff))
.Select(x=>x.Key)
你可以这样写:
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);
本质上:
- 过滤您需要的行(在此时间范围内)
- 按员工编号分组
- 确保组中的 none 行标记为
IsOff
- Select 分组键(在本例中,它将是员工的 ID)
不过请注意,调用对象 Employee
有点令人困惑,因为它实际上代表员工的日程安排,而不是员工本身
也看看这些代码。
我已经定义了一个 Employee 模型 class 来获取数据,
也许目前您需要 Id,但将来您也可以使用其他属性。
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool IsOff { get; set; }
}
和带有 Linq 表达式的代码,
Employee objEmployee = new Employee();
objEmployee.Id = workers
.GroupBy(itm => itm.Id)
.Where(itm => !itm.Any(m => m.IsOff))
.Select(itm => itm.Key)
.FirstOrDefault();
我有一个 Employee
class 定义如下:
Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool isOff { get; set; }
}
这是我的 class 实现和用法:
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},
};
在上面的初始化中只有1条记录是关闭的:
Id = 1, Name = "Emp 1" and the WorkDate = 4/13/2016
现在如何获取整个星期(4 月 11 日至 17 日)没有休息日的员工的 ID?即 Id = 2
LINQ
解决方案要好得多,但我不知道该怎么做。
UPDATED
为了避免混淆对象 Employee
我将其更改为 EmployeeSchedule
class EmployeeSchedule
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool isOff { get; set; }
}
这是实现
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},
};
所选答案仍然适用。
试试,你有两个条件 ID 和日期,但你的 ID 足以满足你的要求,
List<string> resEmp = new List<string>();
foreach (var item in workers )
{
if (item.Id == 2 and item.IsOff == false)
{
resEmp.Add(item);
}
}
这意味着你需要 fromDate
、toDate
,你必须从列表中找到员工,条件是 WorkDate
应该在 [=11] 之间=] 和 toDate
,还有 isOff == false
意思是 has no day off throughout the week
所以你可以使用下面的代码片段:
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();
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
您可以使用 Linq
扩展来实现这一点。
var empid = workers.GroupBy(g=> g.Id)
.Where(x=>x.All(e=>!e.IsOff))
.Select(x=>x.Key)
你可以这样写:
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);
本质上:
- 过滤您需要的行(在此时间范围内)
- 按员工编号分组
- 确保组中的 none 行标记为
IsOff
- Select 分组键(在本例中,它将是员工的 ID)
不过请注意,调用对象 Employee
有点令人困惑,因为它实际上代表员工的日程安排,而不是员工本身
也看看这些代码。
我已经定义了一个 Employee 模型 class 来获取数据,
也许目前您需要 Id,但将来您也可以使用其他属性。
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool IsOff { get; set; }
}
和带有 Linq 表达式的代码,
Employee objEmployee = new Employee();
objEmployee.Id = workers
.GroupBy(itm => itm.Id)
.Where(itm => !itm.Any(m => m.IsOff))
.Select(itm => itm.Key)
.FirstOrDefault();