我正在嘗試計算時間相交的次數的最大值。在開始和結束時間列表中獲取重疊時間的最大值
,我想從下面的代碼示例需要應4.
共有8次是預期的結果,也有6個值,在總相交,一組4,和一組2.
我想得到的是交叉口的最大值,但不能讓它工作。
這是現在的代碼。
void Main()
{
var times = new List<Times> {
new Times
{
Start = DateTime.Now,
End = DateTime.Now.AddMinutes(10)
},
new Times
{
Start = DateTime.Now,
End = DateTime.Now.AddMinutes(10)
},
new Times
{
Start = DateTime.Now.AddMinutes(2),
End = DateTime.Now.AddMinutes(10)
},
new Times
{
Start = DateTime.Now.AddMinutes(15),
End = DateTime.Now.AddMinutes(35)
},
new Times
{
Start = DateTime.Now.AddMinutes(25),
End = DateTime.Now.AddMinutes(42)
},
new Times
{
Start = DateTime.Now.AddMinutes(43),
End = DateTime.Now.AddMinutes(50)
},
new Times
{
Start = DateTime.Now.AddMinutes(55),
End = DateTime.Now.AddMinutes(89)
},
new Times
{
Start = DateTime.Now.AddMinutes(2),
End = DateTime.Now.AddMinutes(12)
}
};
times.OrderBy(x => x.Start);
var overlappingEvents =
(
from e1 in times
where times
.Where(e2 => e1 != e2)
.Where(e2 => e1.Start <= e2.End)
.Where(e2 => e1.End >= e2.Start)
.Any()
select e1).ToList();
overlappingEvents.OrderBy(x => x.Start);
overlappingEvents.Distinct().OrderBy(x => x.Start);
overlappingEvents.Distinct().OrderBy(x => x.Start).Count();
}
public class Times
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
這是時代的輸出對象
Start | End
05/04/2017 08:38:57 | 05/04/2017 08:48:57
05/04/2017 08:38:57 | 05/04/2017 08:48:57
05/04/2017 08:40:57 | 05/04/2017 08:48:57
05/04/2017 08:40:57 | 05/04/2017 08:50:57
05/04/2017 08:53:57 | 05/04/2017 09:13:57
05/04/2017 09:03:57 | 05/04/2017 09:20:57
05/04/2017 09:21:57 | 05/04/2017 09:28:57
05/04/2017 09:33:57 | 05/04/2017 10:07:57
This is the output of the overlapping object (I have added the line where the intersect stops)
Start | End
05/04/2017 08:38:57 | 05/04/2017 08:48:57
05/04/2017 08:38:57 | 05/04/2017 08:48:57
05/04/2017 08:40:57 | 05/04/2017 08:48:57
05/04/2017 08:40:57 | 05/04/2017 08:50:57
---------------------------------------
05/04/2017 08:53:57 | 05/04/2017 09:13:57
05/04/2017 09:03:57 | 05/04/2017 09:20:57
我會很感激,如果有人能幫助我得到的交點的最大值。
謝謝
完美!謝謝 –