I recently had reason to perform an operation on a bunch of files in a directory. I wanted to view a running average iteration time. It would have been easy to use a collection / generic / array, sum the contents and divide by the collection / generic / array length, but I knew there was a simpler way, having done it before. Linq to the rescue! Here the function :
Public Function IterateFiles(path As String) As Double
Dim iterationDurations As New List(Of Double)
For Each fileName As String In IO.Directory.GetFiles(path, "*.*", IO.SearchOption.AllDirectories)
Dim iterationStartTime As Date = Now
' ... Your operation here...
Dim iterationEndTime As Date = Now
iterationDurations.Add((iterationEndTime - iterationStartTime).TotalMilliseconds)
Console.Write("{0} Avg Iteration Time {1}", vbCr, System.Linq.Enumerable.Average(iterationDurations).ToString("F2"))
Next
Return System.Linq.Enumerable.Average(iterationDurations)
End Function