Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a list to Dictionary in VB [closed]

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.

like image 308
Medalla Avatar asked Oct 31 '12 17:10

Medalla


People also ask

How to convert List of KeyValuePair to dictionary?

IDictionary<string, string> dictionary = list. ToDictionary(pair => pair. Key, pair => pair. Value);

How do you empty a dictionary in VB net?

To create an empty dictionary, just go New Dictionary().


1 Answers

Try something like this:-

  Dim list As List(of string)
  Dim dict As IDictionary(Of String) = list.ToDictionary(Function(p) "Id", Function(p) p)
like image 112
Rahul Tripathi Avatar answered Oct 01 '22 10:10

Rahul Tripathi