Medalla Medalla - 3 months ago 65
Vb.net 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.

i.e.

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

Try something like this:-

  Dim list As List(of string)
  Dim dict As IDictionary(Of String) = list.ToDictionary(Function(p) "Id", Function(p) p)