使用LINQ C#计算MTBF

时间:2013-04-04 11:57:04

标签: c# linq

我的数据以下列格式列出((时间值相同,用于样本)

 ID Result      StartTime                EndTime
 1  Fail    4/2/2013 1:17:29 PM     4/2/2013 1:19:30 PM
 2  Fail    4/2/2013 1:17:29 PM     4/2/2013 1:19:30 PM
 3  Pass    4/2/2013 1:17:29 PM     4/2/2013 1:19:30 PM
 4  Fail    4/2/2013 1:17:29 PM     4/2/2013 1:19:30 PM
 5  Fail    4/2/2013 1:17:29 PM     4/2/2013 1:19:30 PM
 6  Pass    4/2/2013 1:17:29 PM     4/2/2013 1:19:30 PM
 7  Fail    4/2/2013 1:17:29 PM     4/2/2013 1:19:30 PM
 8  Pass    4/2/2013 1:17:29 PM     4/2/2013 1:19:30 PM
 9  pass    4/2/2013 1:17:29 PM     4/2/2013 1:19:30 PM
 10  pass   4/2/2013 1:17:29 PM     4/2/2013 1:19:30 PM

我必须使用Σ(T1,T2,... Tn)/ n找到MTBF,其中n是故障的数量,T1,T2,...... Tn是指发生故障之前的正常运行时间。截至目前,我已经能够计算每次迭代的经过时间,

using (DatabaseEntities entities = new DatabaseEntities())
{
    var result = entities.ExecutionDetails.Select(p => p);
    foreach (var p in result)
    {
        if (p.StartTime.HasValue && p.EndTime.HasValue)
        {
            var duration = (p.EndTime.Value - p.StartTime.Value).ToString("c");
            Console.WriteLine("Duration" + duration);
        }
    }
}

您能帮我找到正确的方法和查询来捕获相关数据以计算MTBF。 感谢。

更新代码

var results = entities.TestExecutionDetails.ToList();

            var failures = results.Where(p => p.Result == "Fail").Select(p => p); 

            foreach(var p in failures)
            {
             if (p.StartTime.HasValue && p.EndTime.HasValue)
             {

                 var elapsedduration = elapsedduration + (p.EndTime.Value - p.StartTime.Value).ToString("c");

             }

             var mtbf = elapsedduration / failures.Count();

最终更新

            TimeSpan elaspsedduration = new TimeSpan(0);
             ........
              .....
             foreach (var p in uptime)
            {
                Console.WriteLine("Uptime is" + uptime);
                if (p.StartTime.HasValue && p.EndTime.HasValue)
                {
                    elaspsedduration += (p.EndTime.Value - p.StartTime.Value);
                }
             var mtbf = elaspsedduration.TotalSeconds / failures;

            Console.WriteLine("MTBF Value is " + mtbf);

1 个答案:

答案 0 :(得分:1)

我认为类似的事情可能会起到作用。

var results = entities.ExecutionDetails.ToList();

var failures = results.Where(p => p.Result == "Fail");

var sigmaDiff = failures.Sum(p => p.EndTime.Subtract(p.StartTime));

var mtbf = sigmaDiff/failures.Count();