/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/visual-basic/programming-guide/concepts/linq/snippets/LINQExtension.vb
26 строк
874 B
Bill Wagner
update sample for LINQ how to (#20312)
28 авг 2020, 22:25
Не верифицирован
28 авг 2020, 22:25
83bdd1f
Код
Авторство
О чём код?
Imports System.Runtime.CompilerServices Module LINQExtension ' Extension method for the IEnumerable(of T) interface. ' The method accepts only values of the Double type. <Extension()> Function Median(ByVal source As IEnumerable(Of Double)) As Double If Not source.Any() Then Throw New InvalidOperationException("Cannot compute median for an empty set.") End If Dim sortedSource = (From number In source Order By number).ToList() Dim itemIndex = sortedSource.Count \ 2 If sortedSource.Count Mod 2 = 0 Then ' Even number of items in list. Return (sortedSource(itemIndex) + sortedSource(itemIndex - 1)) / 2 Else ' Odd number of items in list. Return sortedSource(itemIndex) End If End Function End Module