查找时间跨度收藏的平均 [英] Find average of collection of timespans

查看:119
本文介绍了查找时间跨度收藏的平均的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时间跨度的集合,它们代表花在做任务的时间。现在我想找到用在该任务的平均时间。它应该很容易,但由于某种原因,我不是得到正确的平均



下面有什么即时通讯现在做的:

 私人时间跨度? GetTimeSpanAverage(列表<&TimeSpan的GT; SOURCELIST)
{
时间跨度总=默认值(时间跨度);

VAR sortedDates = sourceList.OrderBy(X => X);

的foreach(在sortedDates VAR DATETIME)
{
总+ =日期时间;
}
返回TimeSpan.FromMilliseconds(total.TotalMilliseconds / sortedDates.Count());
}


解决方案

您可以使用平均超载这需要很长的集合中的参数。

 双doubleAverageTicks = sourceList.Average(时间跨度=> timeSpan.Ticks); 
长longAverageTicks = Convert.ToInt64(doubleAverageTicks);

返回新时间跨度(longAverageTicks);


I have collection of timespans, they represent time spent doing a task. Now i would like to find the average time spent on that task. It should be easy but for some reason im not getting the correct average.

Heres what im doing now:

private TimeSpan? GetTimeSpanAverage(List<TimeSpan> sourceList)
{
    TimeSpan total = default(TimeSpan);

    var sortedDates = sourceList.OrderBy(x => x);

    foreach (var dateTime in sortedDates)
    {
        total += dateTime;
    }
    return TimeSpan.FromMilliseconds(total.TotalMilliseconds/sortedDates.Count());
}

解决方案

You can use the Average overload that takes a collection of long in parameter.

double doubleAverageTicks = sourceList.Average(timeSpan => timeSpan.Ticks);
long longAverageTicks = Convert.ToInt64(doubleAverageTicks);

return new TimeSpan(longAverageTicks);

这篇关于查找时间跨度收藏的平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆