我正在尝试从日期列表中创建一组日期范围。
这些日期出现在我的对象中。基本上我会迭代每一行并继续迭代,当时间跨度差异大于5分钟时,我将停止并使用终点作为日期范围。我有一个算法,但问题是它排除了许多数据行:
请参阅下面的示例数据和所需的输出
**Sample Data**
Start_Date Start_Date_Time Replicate
12.12.2012 8:22:58 10
12.12.2012 8:22:58 30
12.12.2012 8:22:58 31
12.12.2012 8:22:58 32
12.12.2012 8:22:58 33
12.12.2012 8:22:58 34
12.14.2012 9:49:27 54
12.14.2012 9:49:27 55
12.14.2012 9:49:27 78
12.14.2012 9:49:27 99
12.14.2012 9:58 120
12.14.2012 9:58 140
12.14.2012 9:58 142
12/12/2012 9:59 144
12/12/2012 9:59 146
12/12/2012 9:59 148
12/12/2012 9:59 150
**Desired Output**
Date Ranges
8:22:58-8:22:58 Replicate10-34
9:49:27-9:49:27 Replicate54-99
9:58-9:59 Replicate120-150
我的代码给了我结果,但它排除了很多行:
lf.ReplicateBlocks.OrderBy(x => x.InitiationDate);
上面的initiateDate是StartDate和Start Time。我按升序对上面的列表进行了排序,从最短的日期/时间开始:
DateTime minimumDateTime = DateTime.MinValue;
foreach (RunLog.Domain.Entities.ReplicateBlock rb in lf.ReplicateBlocks)
{
TimeSpan intervalMinutes = rb.InitiationDate.Subtract(minimumDateTime);
if (intervalMinutes.TotalMinutes >= 5)
{
minimumDateTime = rb.InitiationDate;
//minDates.Add(minimumDateTime);
UserConfirmationErrors confirmationRun = new UserConfirmationErrors();
confirmationRun.minDate = rb.InitiationDate;
confirmationRun.replicateID = rb.ReplicateId;
uc.userConfirmationList.Add(confirmationRun);
}
}
List<RunLog.Domain.Entities.RunLogEntryDatesDisplay> reDisplay = new List<Domain.Entities.RunLogEntryDatesDisplay>();
foreach (var minDate in uc.userConfirmationList)
{
RunLog.Domain.Entities.RunLogEntryDatesDisplay red = new Domain.Entities.RunLogEntryDatesDisplay();
reDisplay.Add(new Domain.Entities.RunLogEntryDatesDisplay() { runDate = minDate.minDate, DateRange = string.Format("{0} - {1}", minDate.minDate, minDate.minDate.AddMinutes(5)), MinimumReplicateId = minDate.replicateID.ToString() });
}
//return reDisplay.OrderByDescending(t => t.runDate).ToList();
return reDisplay;
一旦形成了带有日期范围的用户确认列表,我就会以复选框列表的形式将其发送到视图,用户选择这些日期并选择所选日期并在下面再次查找这些记录:
var query = from d in selectedDates
from o in lf.ReplicateBlocks
where (d.Checked &&
o.InitiationDate >= d.runDate &&
o.InitiationDate <= d.runDate.AddMinutes(5))
select o;
答案 0 :(得分:1)
我会稍微整理一下。首先创建一个表示日期范围的类。有了这里的所有数据,你甚至可以覆盖ToString()
方法来输出你需要的格式。
public class ReplicationDateRange
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int StartId { get; set; }
public int EndId { get; set; }
public override string ToString()
{
return String.Format("{0}-{1} Replicate {2}-{3}", StartDate.ToShortDateString(), EndDate.ToShortDateString(), StartId, EndId);
}
}
然后你需要做的是继续迭代列表,直到你达到一个不在最后一个基线的5分钟内的日期,同时还要更新当前范围的结束日期/ id。以下内容应达到此目的:
var dateRanges = new List<ReplicationDateRange>();
DateTime baselineDate = DateTime.MinValue;
ReplicationDateRange currentDateRange = null;
foreach (var block in lf.ReplicationBlocks.OrderBy(x => x.InitiationDate))
{
if ((block.InitiationDate - baselineDate).TotalMinutes <= 5)
{
currentDateRange.EndDate = block.InitiationDate;
currentDateRange.EndId = block.ReplicateId;
}
else
{
baselineDate = block.InitiationDate;
currentDateRange = new ReplicationDateRange()
{
StartDate = block.InitiationDate,
EndDate = block.InitiationDate,
StartId = block.ReplicateId,
EndId = block.ReplicateId
};
dateRanges.Add(currentDateRange);
}
}
foreach (var d in dateRanges)
{
Console.WriteLine(d);
}