本文介绍了如何从C#中的列表集合中获取总持续时间(以分钟为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[TestMethod]
public void GetTotalDurationFor_PassEventInBetween()
{
new List<EventItemModel>()
{
new EventItemModel() {DateTime = new DateTime(2017,1,1,10,0,0), Event = EventType.Enter},
new EventItemModel() {DateTime = new DateTime(2017,1,1,10,30,0), Event = EventType.Pass},
new EventItemModel() {DateTime = new DateTime(2017,1,1,11,30,0), Event = EventType.Leave},
}.GetTotalDurationFor().Should().Be(90);
}
}
THIS TEST FAILES ...
PLEASE HELP
我尝试过:
What I have tried:
public static double GetTotalDurationFor(this IEnumerable<EventItemModel> lst)
{
var selectedEmployeDayinout = lst.OrderBy(d => d.DateTime).ToList();
const int enterExitOperations = 2;
double duration = 0;
if ((selectedEmployeDayinout.Count()%enterExitOperations) != 0) return duration;
for (var i = 0; i < selectedEmployeDayinout.Count(); i ++)
{
var enterDate = selectedEmployeDayinout[i].DateTime;
var leaveDate = selectedEmployeDayinout[i + 1].DateTime;
duration += leaveDate.Subtract(enterDate).TotalSeconds;
}
return duration;
}
推荐答案
这篇关于如何从C#中的列表集合中获取总持续时间(以分钟为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!