How to calculate the average of time values in a datatable

Hi,

I have a data table that contains run-times (eg 0:01:08) and would like to seek your help to calculate their average.

I have done FilteredData.AsEnumerable.Average(Function(row) CDate(row(“Elapse”))) but it seems this does not work with CDate.

Thanks!

Hi @jepviterbo ,

Could you try with the below Expression :

First we will measure the Average by using the TotalSeconds of the Time. Then calculate it’s average and find it’s TimeSpan value using TimeSpan.FromSeconds

Timespan.FromSeconds(FilteredData.AsEnumerable.Average(Function(row)Timespan.Parse(row("Elapse").ToString).TotalSeconds))

The above Expression should give you the Average time.

Let us know if this is not the expected answer.

HI @jepviterbo

Try this expression

(From d In DtMaster.AsEnumerable Where Not (isNothing(d("Elapse")) OrElse String.IsNullorEmpty(d("Elapse").toString.Trim)) Select v = CDbl(d("Elapse").toString.Trim)).Average(Function (x) x)

Regards
Gokul

Hi @jepviterbo ,

Here is an alternative, if you want the Average TimeSpan

image

TimeSpan.FromSeconds(dt.AsEnumerable().Where(Function(w) Not(IsNothing(w(ColumnNameOrIndex)) OrElse String.IsNullOrEmpty(w(ColumnNameOrIndex).ToString))).Average(Function(s) TimeSpan.Parse(s(ColumnNameOrIndex)).TotalSeconds))

If you want the average seconds →

dt.Where(Function(w) Not(IsNothing(w(ColumnNameIndex)) OrElse String.IsNullOrEmpty(w(ColumnNameIndex).ToString))).Average(Function(s) TimeSpan.Parse(s(ColumnNameIndex)).TotalSeconds)

Kind Regards,
Ashwin A.K