2

我正在尝试从日期列表中创建一组日期范围。

这些日期存在于我的对象中。基本上我会遍历每一行并继续迭代,当时间跨度差异大于 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;
4

1 回答 1

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);
}
于 2013-01-22T16:53:50.383 回答