在开始时间和结束时间列表中获取重叠时间的最大值
Get Max Value of overlapping times in a list of Start and End Times
我正在尝试计算时间相交的最大值。
我希望从下面的代码示例中得到的预期结果应该是 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
如果有人能帮我得到交叉点的最大值,我将不胜感激。
谢谢
试试这个。我假设如果两个间隔至少有一个共同点(即矿石间隔的开始等于另一个间隔的结束),则它们是重叠的。
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)
}
};
var overlaps = times.Select(t1 => times.Count(t2 => IsOverlapping(t1, t2))).Max();
}
bool IsOverlapping(Times t1, Times t2)
{
if (t1.Start >= t2.Start && t1.Start <= t2.End)
{
return true;
}
if (t1.End >= t2.Start && t1.End <= t2.End)
{
return true;
}
if (t2.Start >= t1.Start && t2.Start <= t1.End)
{
return true;
}
if (t2.End >= t1.Start && t2.End <= t1.End)
{
return true;
}
return false;
}
public class Times
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
IsOverlapping
函数可以简化,如果你可以改变Times
class:
bool IsOverlapping(Times t1, Times t2)
{
return t1.Contains(t2.Start) || t1.Contains(t2.End) || t2.Contains(t1.Start) || t2.Contains(t1.End);
}
public class Times
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public bool Contains(DateTime date)
{
return date >= Start && date <= End;
}
}
在 class
中再添加一个 属性
public class Times
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public TimeSpan Gap { get; set; }
}
计算差距
times.OrderBy(x => x.Start);
for (int i = 0; i < times.Count-1; i++)
{
times[i].Gap = times[i+1].Start - times[i].End;
}
times.OrderByDescending(x => x.Gap);
我正在尝试计算时间相交的最大值。
我希望从下面的代码示例中得到的预期结果应该是 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
如果有人能帮我得到交叉点的最大值,我将不胜感激。
谢谢
试试这个。我假设如果两个间隔至少有一个共同点(即矿石间隔的开始等于另一个间隔的结束),则它们是重叠的。
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)
}
};
var overlaps = times.Select(t1 => times.Count(t2 => IsOverlapping(t1, t2))).Max();
}
bool IsOverlapping(Times t1, Times t2)
{
if (t1.Start >= t2.Start && t1.Start <= t2.End)
{
return true;
}
if (t1.End >= t2.Start && t1.End <= t2.End)
{
return true;
}
if (t2.Start >= t1.Start && t2.Start <= t1.End)
{
return true;
}
if (t2.End >= t1.Start && t2.End <= t1.End)
{
return true;
}
return false;
}
public class Times
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
IsOverlapping
函数可以简化,如果你可以改变Times
class:
bool IsOverlapping(Times t1, Times t2)
{
return t1.Contains(t2.Start) || t1.Contains(t2.End) || t2.Contains(t1.Start) || t2.Contains(t1.End);
}
public class Times
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public bool Contains(DateTime date)
{
return date >= Start && date <= End;
}
}
在 class
中再添加一个 属性 public class Times
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public TimeSpan Gap { get; set; }
}
计算差距
times.OrderBy(x => x.Start);
for (int i = 0; i < times.Count-1; i++)
{
times[i].Gap = times[i+1].Start - times[i].End;
}
times.OrderByDescending(x => x.Gap);