Occasionally I run into situations where I have a List of objects that I need to cast to a list of specific types. One option would be to iterate through the list of objects and cast each item individually and add them to a new list of the specific type.
- Dim countries As New List(Of Country)
- For Each item As Object In items
- countries.Add(DirectCast(item, Country))
- Next
Another more cleaner option would be to use a Linq statement.
- Dim countries As New List(Of Country)
- countries = items.Cast(Of Country).ToList()