Medalla Medalla - 1 year ago 190 Question

Converting a list to Dictionary in VB

I have a list of strings.
I want to create a Dictionary of string, string, with "Id" as the key for each entry and the contents of the list as the value.


myList= { "string1", "string2", ...etc }

and therefore

myDictionary = {{"Id1", "string1"}, {"Id2", "string2"}, ...etc}

I have been trying to create a dictionary using the List.ToDictionary method but to no avail

List.ToDictionary(Of String, String)("Id", Function(p) p.key)

Any help is much appreciated.

Answer Source

Try something like this:-

  Dim list As List(of string)
  Dim dict As IDictionary(Of String) = list.ToDictionary(Function(p) "Id", Function(p) p)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download