在 List<T> 中选择一周内没有休息日的记录 - C#
Posted
技术标签:
【中文标题】在 List<T> 中选择一周内没有休息日的记录 - C#【英文标题】:Select records which has no day-off throughout the week in List<T> - C# 【发布时间】:2016-08-02 14:16:17 【问题描述】:我有一个 Employee
类,定义如下:
Employee
public int Id get; set;
public string Name get; set;
public DateTime WorkDate get; set;
public bool isOff get; set;
这是我的类实现和用法:
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
解决方案要好得多,但我不知道该怎么做。
更新
为避免混淆对象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,
;
所选答案仍然适用。
【问题讨论】:
Lamda 表达式可以吗?var list = workers.Where(x => x.IsOff == false)
我不知道必须调用什么扩展来过滤Id = 2
@Bharat 我需要LINQ
先生。
我已经添加了我的答案,..@***.com/users/6143656/shift-n-tab
【参考方案1】:
现在我如何获取没有休息日的员工的 ID 整个星期(从 4 月 11 日至 17 日)? Id = 2
您可以使用Linq
扩展,并实现此目的。
var empid = workers.GroupBy(g=> g.Id)
.Where(x=>x.All(e=>!e.IsOff))
.Select(x=>x.Key)
【讨论】:
在处理分组元素时,您需要将.Select(x=>x.Id)
更改为.Select(x=>x.Key)
。【参考方案2】:
也看看这些代码。
我已经定义了一个 Employee 的 Model 类来获取数据,
也许目前您需要 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();
【讨论】:
这个也很有用【参考方案3】:这意味着您需要一个fromDate
、toDate
,并且您必须从列表中找到员工,条件是,WorkDate
应该在fromDate
和toDate
之间,并且还有@ 987654326@ 表示has no day off throughout the week
所以你可以使用下面的sn-p:
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();
【讨论】:
这并不是说他们在那段时间没有休息日。它将返回未关闭的日期,而不是 ID @Rob 只需要一个 GroupBy(g=>x.id)【参考方案4】:你可以这样写:
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);
基本上:
-
过滤您需要的行(在此时间范围内)
按员工 ID 分组
确保组中没有行标记为
IsOff
选择分组键(在这种情况下,它将是员工的 ID)
但请注意,调用对象 Employee
有点令人困惑,因为它真正代表的是员工的日程安排,而不是员工本身
【讨论】:
【参考方案5】:试试看,你有两个条件 id 和 date,但你的 id 足以满足你的要求,
List<string> resEmp = new List<string>();
foreach (var item in workers )
if (item.Id == 2 and item.IsOff == false)
resEmp.Add(item);
【讨论】:
以上是关于在 List<T> 中选择一周内没有休息日的记录 - C#的主要内容,如果未能解决你的问题,请参考以下文章