C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ToList: We need to invoke ToList, another extension, after calling Distinct to convert back into a list.
ToListNote: Distinct returns an IEnumerable collection, not a List. But with ToList we convert it easily back into a List.
IEnumerableVB.NET program that filters duplicates
Module Module1
Sub Main()
' Create list and add values.
Dim values As List(Of Integer) = New List(Of Integer)
values.Add(1)
values.Add(2)
values.Add(2)
values.Add(3)
values.Add(3)
values.Add(3)
' Filter distinct elements, and convert back into list.
Dim result As List(Of Integer) = values.Distinct().ToList
' Display result.
For Each element As Integer In result
Console.WriteLine(element)
Next
End Sub
End Module
Output
1
2
3
Tip: Distinct will also work on string values and other value types. For objects, an equality comparer is needed on the type.