vb.net VB .net 获取字典项作为 KeyValuePair

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25619465/
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 18:09:02  来源:igfitidea点击:

VB .net get Dictionary item as KeyValuePair

vb.netdictionary

提问by dba

might be a noob question... For me it seems to be obvious, that a dictionary might have a method for getting it's item by Key.... Either I am blind, or... However, I have following situation:

可能是一个菜鸟问题......对我来说,这似乎很明显,字典可能有一种通过键获取项目的方法......要么我是盲人,要么......但是,我有以下情况:

A need a dictionary (of integer, k/v). I have the k/v's already in a dictionary and liked to Add them to my new one by Key, such as:

需要一个字典(整数,k/v)。我在字典中已经有了 k/v,并且喜欢通过 Key 将它们添加到我的新字典中,例如:

'Dict1 has {("Key1", 123), ("Key2", 345)}
dim dict2 as New dictionary (of integer, KeyValuePair(of String, Double))

dict2.add(0,***dict1.GetItem("Key1")***)

Sure I could write an Extension, and probably I'll have to, but.... is it only me missing this function...? If there was a built in way I've overseen, so please point me there!

当然我可以写一个扩展,而且可能我必须这样做,但是....只有我缺少这个功能吗...?如果有我监督过的内置方式,请指点我!

Thanks in advance, Daniel

提前致谢,丹尼尔

EDIT:

编辑:

'dict1={(0, "apples"), (1,"oranges"), (2, "bananas")}
'dict2={("oranges",456), ("apples", 123),("bananas",789)}

the new dictionary is a sorted dict2 by dict1

新字典是按 dict1 排序的 dict2

'dictnew={("apples", 123),("oranges",456),("bananas",789)}

So my approach was,

所以我的方法是,

'dicttemp={(0, ("apples", 123)),(1,("oranges",456)),(2,("bananas",789))} 'dicttemp is a sorteddictionary

next I get the values of temp and transform them to a dict.. Maybe my approach is bad at all :-)

接下来,我获取 temp 的值并将它们转换为 dict .. 也许我的方法根本不好:-)

回答by Matías Fidemraizer

You're looking for dictionary's indexer.

您正在寻找字典的 indexer

Dim a As Double = dict1("Key1")

In some comment, OP said:

在一些评论中,OP 说:

I need the whole item as k/v and add the item of dict1 to dict2!!!

我需要整个项目作为 k/v 并将 dict1 的项目添加到 dict2 !

Since a dictionary implements IEnumerable(Of T)where Tis KeyValuePair(Of TKey, TValue), you can use Single(...)LINQ extension method to find out a key-value pair by key:

由于字典实现IEnumerable(Of T)where Tis KeyValuePair(Of TKey, TValue),您可以使用Single(...)LINQ 扩展方法按键查找键值对:

Dim pair As KeyValuePair(Of String, Double) = dict1.Single(Function(item) item.Key = "key")

Since dictionary's Addmethod has an overload to give a key-value pair as argument, I believe that adding dict1key-value pair to dict2is just about calling Addin the second dictionary providing the obtained key-value pair from first dictionary:

由于字典的Add方法有一个重载来提供键值对作为参数,我相信添加dict1键值对dict2只是调用Add第二个字典提供从第一个字典获得的键值对:

dict2.Add(pair);

回答by Tim Schmelter

So you have already the key, the only part missing is the value. Therefore you use the indexer or TryGetValue. Then you have both parts and you can add them to the other dictionary.

所以你已经有了钥匙,唯一缺少的就是价值。因此,您使用索引器或TryGetValue. 然后你有两个部分,你可以将它们添加到另一个字典中。

Dim dict1 As New Dictionary(Of String, Double) From {{"Key1", 123}, {"Key2", 345}}
Dim dict2 As New Dictionary(Of Integer, KeyValuePair(Of String, Double))
Dim keyVal = New KeyValuePair(Of String, Double)("Key1", dict1("Key1"))
dict2.Add(0, keyVal)

Here is the TryGetValueapproach:

这是TryGetValue方法:

Dim d As Double
If dict1.TryGetValue("Key1", d) Then
    dict2.Add(0, New KeyValuePair(Of String, Double)("Key1", d))
End If

For the sake of completeness, you could use LINQ although this would be slow and pointless:

为了完整起见,您可以使用 LINQ,尽管这会很慢且毫无意义:

Dim keyVal = dict1.FirstOrDefault(Function(kv)kv.Key = "Key1")

Now you are using a dictionary like a collection which goes against the purpose of the class. This actually loops all items until one is found with a given key.

现在您正在使用字典之类的集合,这违背了课程的目的。这实际上会循环所有项目,直到找到具有给定键的项目为止。



For what it's worth, since you've asked for a method that returns a KeyValuePairfor a given key. Here is an extension method:

对于它的价值,因为您已经要求一种KeyValuePair为给定键返回 a 的方法。这是一个扩展方法:

<Extension()>
Public Function TryGetKeyValue(Of TKey, TValue)(dictionary As Dictionary(Of TKey, TValue), key As TKey) As KeyValuePair(Of TKey, TValue)?
    Dim value As TValue
    If dictionary.TryGetValue(key, value) Then
        Return New KeyValuePair(Of TKey, TValue)(key, value)
    End If
    Return Nothing
End Function

You use it in this way:

您以这种方式使用它:

Dim keyVal As KeyValuePair(Of String, Double)? = dict1.TryGetKeyValue("Key1")
If keyVal.HasValue Then dict2.Add(0, keyVal.Value)

It is a O(1) operation, so it's more efficient than using LINQ.

这是一个 O(1) 操作,因此它比使用 LINQ 更有效。