在VB中将列表转换为字典

Med*_*lla 5 vb.net dictionary list

我有一个字符串列表。我想创建一个字符串字典,字符串,以“Id”作为每个条目的键,以列表的内容作为值。

IE

myList= { "string1", "string2", ...etc }
Run Code Online (Sandbox Code Playgroud)

因此

myDictionary = {{"Id1", "string1"}, {"Id2", "string2"}, ...etc}
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用 List.ToDictionary 方法创建字典但无济于事

List.ToDictionary(Of String, String)("Id", Function(p) p.key)
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢。

Rah*_*thi 6

尝试这样的事情:-

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

  • 这是。此外,OP 要求 _all_ 键为“Id”,这有点打破了字典的想法...... (3认同)