从LINQ到SQL的开始结束时间列表的平均时间[英] Average time from a list of start end times in linq to sql

本文是小编为大家收集整理的关于从LINQ到SQL的开始结束时间列表的平均时间的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有

列表
*startTime    (datetime)
*endTime      (datetime)

,需要确定列表中所有内容的平均时间.

所以我猜我需要

之类的东西
long averageTime = 
 Convert.ToInt64(listOfStartEndTimes.Average(timeSpan => timeSpan.Ticks))

有什么想法?

推荐答案

long averageTime = listOfStartEndTimes
                       .Select(se => se.End - se.Start)
                       .Average(t => t.Ticks);

或其他分辨率略低(unix时期日期):

long averageTime = listOfStartEndTimes
                       .Select(se => se.End - se.Start)
                       .Average(t => (t.Ticks – 621355968000000000) / 10000000);

其他推荐答案

其他答案是正确的,您可以使用简单的LINQ平均TimeSpan属性.我更喜欢让TimeSpan班级做它的工作来确定精度,例如Uffe所示.但是请确保您使用的是正确的属性. .Seconds将返回不适合整个分钟的秒数的剩余.您想要.TotalSeconds属性.

使用.Ticks可能很诱人,但是如果您有很多值并且彼此之间的间距足够远,则可以轻松获得OverflowException.我的建议是在结果中使用比您需要的小单元.因此,如果您关心平均精度到分钟,那么.TotalSeconds应该很好地工作.然后,如果您喜欢的话,可以将结果带回TimeSpan.

var sec = yourList.Select(p => p.Stop - p.Start).Average(p => p.TotalSeconds);
var avg = TimeSpan.FromSeconds(sec);

另外,由于您的Start和Stop值是类型DateTime,因此您确实需要注意他们的.Kind.这样的操作只有在yourDateTime.Kind == DateTimeKind.Utc

的情况下保证是准确的

如果它们是Local种类,则结果将受到运行代码的本地计算机时区的影响.如果它们是Unspecified,则数据可能已记录在本地时区或其他时区的上下文中.如果您在掩护日光节省时间过渡中工作,那么减法的结果可能是不正确的.

例如,如果您要在美国太平洋时区运行此代码,并且您有Local的2013-11-03 06:00,则减去2013-11-03 06:00 - 2013-11-03 00:00会给您6个小时的TimeSpan .但实际上,7个小时将过去7个小时,因为那是DST结束的一天,而时钟在1:00至2:00之间重复一小时.

要避免此问题,您应该只使用Utc中的DateTime值进行数学,否则您应该使用DateTimeOffset值代替.

其他推荐答案

如果您有类

public class Time
{
    public DateTime Start;
    public DateTime Stop;

    public Time(DateTime start, DateTime stop)
    {
        this.Start = start;
        this.Stop = stop;
    }
}

您可以用DateTime值填充您可以使用...

获得平均值
var avg = Times.Select(p => p.Stop - p.Start).Average(p => p.Seconds);

您可以在哪里替换P.Seconds用您想要的任何东西..小时,分钟等... 注意:未经测试,但我认为它可能起作用

编辑:啊,已经回答:)

本文地址:https://www.itbaoku.cn/post/1556804.html

问题描述

I have a list of

*startTime    (datetime)
*endTime      (datetime)

and need to work out the average time for everything in the list.

So I am guessing I need something like

long averageTime = 
 Convert.ToInt64(listOfStartEndTimes.Average(timeSpan => timeSpan.Ticks))

Any ideas?

推荐答案

long averageTime = listOfStartEndTimes
                       .Select(se => se.End - se.Start)
                       .Average(t => t.Ticks);

Or alternatively, with a slightly lower resolution (unix epoch date instead):

long averageTime = listOfStartEndTimes
                       .Select(se => se.End - se.Start)
                       .Average(t => (t.Ticks – 621355968000000000) / 10000000);

其他推荐答案

The other answers are correct that you can average TimeSpan properties with simple LINQ. I prefer letting the TimeSpan class do it's work to determine precision, such as Uffe showed. But make sure you are using the correct property. .Seconds will return the remainder of seconds that don't fit into a full minute. You want the .TotalSeconds property.

Using .Ticks might be tempting, but you could easily get an OverflowException if you have a lot of values and they are spaced far enough apart from each other. My recommendation is to use one unit smaller than you need in your result. So if you care about average precision to the minute, then .TotalSeconds should work well. You can then take the result back into a TimeSpan if you like.

var sec = yourList.Select(p => p.Stop - p.Start).Average(p => p.TotalSeconds);
var avg = TimeSpan.FromSeconds(sec);

Also, since your Start and Stop values are of type DateTime, then you really need to pay attention to their .Kind. An operation like this is only guaranteed to be accurate if yourDateTime.Kind == DateTimeKind.Utc.

If they are Local kinds, then the result will be influenced by the time zone of the local computer that is running the code. If they are Unspecified, then the data may have been recorded in the context of the local time zone, or of some other time zone. If the times you are working in cover a daylight saving time transition, then your result of subtraction may be incorrect.

For example, if you are running this code in the US Pacific Time zone, and you have Local kinds of DateTime, then subtracting 2013-11-03 06:00 - 2013-11-03 00:00 would give you a TimeSpan of 6 hours. But in reality, 7 hours will have elapsed, since that is the day DST ends and the clocks repeat the hour between 1:00 and 2:00.

To avoid this problem, you should only do math with DateTime values that are in Utc, or you should use DateTimeOffset values instead.

其他推荐答案

If you have a class

public class Time
{
    public DateTime Start;
    public DateTime Stop;

    public Time(DateTime start, DateTime stop)
    {
        this.Start = start;
        this.Stop = stop;
    }
}

That you fill with datetime values you can probably get the average with...

var avg = Times.Select(p => p.Stop - p.Start).Average(p => p.Seconds);

Where you can replace p.Seconds with whatever you would like.. Hours, minutes etc... Note: Not tested, but I think it might work

Edit: Ahh, already answered :)