Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 扩展法求和时间跨度简化_C#_Linq_Extension Methods - Fatal编程技术网

C# 扩展法求和时间跨度简化

C# 扩展法求和时间跨度简化,c#,linq,extension-methods,C#,Linq,Extension Methods,我找到了这个扩展方法 /// <summary> /// Calculates the sum of the given timeSpans. /// </summary> public static TimeSpan Sum(this IEnumerable<TimeSpan> timeSpans) { TimeSpan sumTillNowTimeSpan = TimeSpan.Zero; foreach (TimeSpan timeSp

我找到了这个扩展方法

/// <summary>
/// Calculates the sum of the given timeSpans.
/// </summary>
public static TimeSpan Sum(this IEnumerable<TimeSpan> timeSpans)
{
    TimeSpan sumTillNowTimeSpan = TimeSpan.Zero;

    foreach (TimeSpan timeSpan in timeSpans)
    {
        sumTillNowTimeSpan += timeSpan;
    }
    return sumTillNowTimeSpan;           
}

由于
Sum()
执行内部
+=
所有值,我希望能够编译此文件-对他的好奇是Visual Studio的intellisense建议我使用
Sum()
,并且存在一个重载,但它无法编译。

因此以下内容无法编译

List<TimeSpan> timeSpans = new List<TimeSpan>();
timeSpans.Add(TimeSpan.FromHours(1));
timeSpans.Add(TimeSpan.FromHours(2));
timeSpans.Add(TimeSpan.FromHours(3));
TimeSpan sum = timeSpans.Sum();// will be 06:00
编辑:正如《将军》所指出的,如果纳秒是一个问题,最好使用Ticks long属性来避免舍入错误

TimeSpan sum = TimeSpan.FromTicks(timeSpans.Sum(t => t.Ticks));
虽然我认为我实际上更喜欢kurakura88的答案,因为它使用了TimeSpan的Add方法,这应该比任何其他可以用来添加TimeSpan的方法都更可靠,我喜欢Mick的答案(投了赞成票),尽管我使用了ticks,所以不会失去精确度

如果你有可打印的性格强迫症和强迫症,你可以把它放在你自己的可怕的扩展

public static class TimeSpanExtensions
{
   public static TimeSpan Sum(this IEnumerable<TimeSpan> source)
      => TimeSpan.FromTicks(source.Sum(t => t.Ticks));
}
公共静态类TimeSpanExtensions
{
公共静态时间跨度总和(此IEnumerable源)
=>TimeSpan.FromTicks(source.Sum(t=>t.Ticks));
}
用法

List<TimeSpan> timeSpans = new List<TimeSpan>();
timeSpans.Add(TimeSpan.FromHours(1));
timeSpans.Add(TimeSpan.FromHours(2));
timeSpans.Add(TimeSpan.FromHours(3));
TimeSpan sum = timeSpans.Sum();
List timespan=new List();
timeSpans.Add(TimeSpan.FromHours(1));
TimeSpan.Add(TimeSpan.FromHours(2));
TimeSpan.Add(TimeSpan.FromHours(3));
TimeSpan sum=TimeSpan.sum();

由于
Linq
Sum
不可用于
IEnumerable
,另一种选择是使用
聚合

TimeSpan sum = timespans.Aggregate(TimeSpan.Zero, (t1,t2) => t1.Add(t2));

你试过运行代码吗?你得到正确的结果了吗?如果是,那么它是有用的。你能解释一下有用性的背景吗?这不是一个问题。。。如果您需要它,您必须评估它是否对您有用。显然,如果您想要执行timeSpans.Sum()的行,您需要一个方法extensionIts,它工作得很好。问题是什么?@GaurangDave我已经更新了问题-也许现在你更清楚了
public static class TimeSpanExtensions
{
   public static TimeSpan Sum(this IEnumerable<TimeSpan> source)
      => TimeSpan.FromTicks(source.Sum(t => t.Ticks));
}
List<TimeSpan> timeSpans = new List<TimeSpan>();
timeSpans.Add(TimeSpan.FromHours(1));
timeSpans.Add(TimeSpan.FromHours(2));
timeSpans.Add(TimeSpan.FromHours(3));
TimeSpan sum = timeSpans.Sum();
TimeSpan sum = timespans.Aggregate(TimeSpan.Zero, (t1,t2) => t1.Add(t2));