vb.net 在VB中将列表转换为字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13163666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 10:58:13  来源:igfitidea点击:

Converting a list to Dictionary in VB

vb.netlistdictionary

提问by Medalla

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.

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

i.e.

IE

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 方法创建字典但无济于事

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

Any help is much appreciated.

任何帮助深表感谢。

回答by Rahul Tripathi

Try something like this:-

尝试这样的事情:-

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

回答by Jon B

I want to create a Dictionary of string, string, with "Id" as the key for each entry

我想创建一个字符串字典,字符串,以“Id”作为每个条目的键

This is impossible. Every entry in the dictionary must have a unique key.

这是不可能的。字典中的每个条目都必须有一个唯一的键。