<Extension()> _
Function StandardDeviation( _
  ByVal source As IEnumerable(Of Double)) As Double

  If source Is Nothing Then
    Throw New ArgumentNullException("source")
  End If

  If source.Count = 0 Then
    Throw New InvalidOperationException("Nelze potat standardn 
odchylku pro przdnou mnoinu.")
  End If

  Dim avg = Aggregate v In source Into Average(v)
  Dim accumulator As Double = 0

  For Each x In source
    accumulator += (x - avg) ^ 2
  Next

  Return Math.Sqrt(accumulator / (source.Count))

End Function

<Extension()> _
Function StandardDeviation(Of TSource)( _
    ByVal source As IEnumerable(Of TSource), _
    ByVal selector As Func(Of TSource, Double)) As Double

  Return (From element In source Select _
    selector(element)).StandardDeviation()

End Function

Sub Main()

  Dim expr = Aggregate p In products _
    Into StandardDeviation(p.Price)

End Sub
