Lookup(TKey, TElement) Represents a collection of keys each mapped to one or more values.
Structure Product
Public Company As String
Public Weight As Double
Public TrackingNumber As Long
End Structure
Public Class Example
Public Shared Sub Main()
Dim packages As New System.Collections.Generic.List(Of Product)(New Product() _
{New Product With {.Company = "A", .Weight = 25.2, .TrackingNumber = 1L}, _
New Product With {.Company = "B", .Weight = 18.7, .TrackingNumber = 2L}, _
New Product With {.Company = "C", .Weight = 6.0, .TrackingNumber = 3L}, _
New Product With {.Company = "D", .Weight = 9.3, .TrackingNumber = 4L}, _
New Product With {.Company = "E", .Weight = 33.8, .TrackingNumber = 5L}})
Dim lookup As ILookup(Of Char, String) = packages.ToLookup(Function(ByVal p) Convert.ToChar(p.Company.Substring(0, 1)), _
Function(ByVal p) p.Company & " " & p.TrackingNumber)
For Each packageGroup As IGrouping(Of Char, String) In lookup
Console.WriteLine(packageGroup.Key)
For Each str As String In packageGroup
Console.WriteLine(str)
Next
Next
Dim count As Integer = lookup.Count
Dim cgroup As System.Collections.Generic.IEnumerable(Of String) = lookup("C"c)
For Each str As String In cgroup
Console.WriteLine(str)
Next
Dim hasG As Boolean = lookup.Contains("G"c)
End Sub
End Class
Related examples in the same category