我正在尝试从日期列表中创建一组日期范围。
这些日期存在于我的对象中。基本上我会遍历每一行并继续迭代,当时间跨度差异大于 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);
上面的initiationDate 是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;