C# 在具有开始/结束日期的对象列表中查找间隙
我有一个物品清单,上面有一个人收费的开始和结束日期C# 在具有开始/结束日期的对象列表中查找间隙,c#,C#,我有一个物品清单,上面有一个人收费的开始和结束日期 public class ProjectResourceCostDto { public int Id { get; set; } public int ProjectResourceId { get; set; } public DateTime StartDate { get; set; } public DateTime? EndDate { get; set; }
public class ProjectResourceCostDto
{
public int Id { get; set; }
public int ProjectResourceId { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public decimal Cost { get; set; }
public bool Deleted { get; set; }
}
我有一个项目的开始和结束日期。所以我需要做的是返回一个“间隙”列表,其中没有设置速率
因此,如果我的项目开始日期为2013年1月1日,结束日期为2013年12月31日,则它们是我的输入
我需要浏览列表,并输出没有付款率的开始/结束日期列表
因此,如果我的对象列表具有:
开始日期=2013年1月5日
完2013年10月1日
开始日期=2013年10月15日
完2013年12月25日
然后,我需要返回:
2013年1月1日
2013年1月4日
2013年10月2日
2013年10月14日
2013年12月26日
2013年12月31日
这是我无法确定利率的时期
这是用C代码完成的。我正在使用实体框架,所以另一个选择可能是SQL Server中的视图,我可以利用它。。。我有一个日历表,上面有所有的日期。。。但是,如果有人有一个我可以在代码中使用的例程来计算这些时间段,那将是头奖。例如,您可以按日期开始日期对输入列表进行排序,然后在每次满足目标条件(即成本=0)时,通过将日期添加到新列表来迭代输入列表。这里有一个将所有相关日期写入gapsList的示例代码: 请尝试此库:
List<ProjectResourceCostDto> testList = new List<ProjectResourceCostDto>();
testList.Add(new ProjectResourceCostDto{Id = 1, ProjectResourceId = 1, StartDate = new DateTime(2001,2,1), EndDate = new DateTime(2002, 1,1), Cost = 1111, Deleted = false});
testList.Add(new ProjectResourceCostDto{Id = 2, ProjectResourceId = 2, StartDate = new DateTime(2003,1,1), EndDate = new DateTime(2004, 1,1), Cost = 0, Deleted = false});
testList.Add(new ProjectResourceCostDto { Id = 3, ProjectResourceId = 3, StartDate = new DateTime(2005, 1, 1), EndDate = new DateTime(2006, 1, 1), Cost = 999, Deleted = false });
DateTime firstDate = new DateTime(2001, 1, 1);
DateTime lastDate = new DateTime(2006, 2, 1);
List<DateTime> gapsList = new List<DateTime>();
gapsList.Add(firstDate);
testList = testList.OrderBy(n => n.StartDate).ToList();
foreach(ProjectResourceCostDto item in testList)
{
if (item.Cost == 0)
{
gapsList.Add(item.StartDate);
gapsList.Add((DateTime)item.EndDate);
}
}
gapsList.Add(lastDate);