Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 获取字典项作为KeyValuePair_Vb.net_Dictionary_Keyvaluepair - Fatal编程技术网

Vb.net 获取字典项作为KeyValuePair

Vb.net 获取字典项作为KeyValuePair,vb.net,dictionary,keyvaluepair,Vb.net,Dictionary,Keyvaluepair,对我来说,很明显,字典可能有一种方法可以通过键获取它的条目。不是我瞎了,就是。。。 不过,我有以下情况: 需要一个字典(整数,k/v)。我的k/v已经在字典中,我想通过键将它们添加到我的新字典中,例如: 'Dict1 has {("Key1", 123), ("Key2", 345)} Dim dict2 As New Dictionary (Of Integer, KeyValuePair(Of String, Double)) dict2.Add(0,

对我来说,很明显,字典可能有一种方法可以通过
获取它的条目。不是我瞎了,就是。。。 不过,我有以下情况:

需要一个
字典(整数,k/v)
。我的
k/v
已经在
字典中,我想通过
键将它们添加到我的新字典中,例如:

'Dict1 has {("Key1", 123), ("Key2", 345)}
Dim dict2 As New Dictionary (Of Integer, KeyValuePair(Of String, Double))
dict2.Add(0,***dict1.GetItem("Key1")***)
当然我可以写一个扩展,也许我必须写,但是只有我缺少这个函数吗? 如果有一个内置的方式,我监督,请告诉我那里

提前感谢,

丹尼尔


编辑:

新词典是按
dict1
排序的
dict2

dictnew = {("apples", 123), ("oranges", 456), ("bananas", 789)}
所以我的方法是:

dicttemp = {(0, ("apples", 123)), (1, ("oranges", 456)), (2, ("bananas", 789))}'. `dicttemp` is a `SortedDictionary`.
接下来,我获取dicttemp的值,并将它们转换为您正在查找的
字典

OP在一些评论中说:

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

由于字典实现了
IEnumerable(Of T)
,其中
T
KeyValuePair(Of TKey,TValue)
,因此可以使用
Single(…)
LINQ扩展方法按键查找键值对:

Dim pair As KeyValuePair(Of String, Double) = dict1.Single(Function(item) item.Key = "key")
由于dictionary的
Add
方法有一个重载,可以提供一个键值对作为参数,因此我认为将
dict1
键值对添加到
dict2
就是在第二个字典中调用
Add
,提供从第一个字典中获得的键值对:

dict2.Add(pair);

因此,您已经有了密钥,唯一缺少的部分是值。因此,您可以使用索引器或
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)
以下是
TryGetValue
方法:

Dim d As Double
If dict1.TryGetValue("Key1", d) Then
    dict2.Add(0, New KeyValuePair(Of String, Double)("Key1", d))
End If
为了完整性起见,您可以使用LINQ,尽管这会很慢而且毫无意义:

Dim keyVal = dict1.FirstOrDefault(Function(kv)kv.Key = "Key1")
现在,您使用的字典就像一个集合,这与类的目的背道而驰。这实际上会循环所有项,直到找到一个具有给定键的项为止


因为您要求提供一个方法,该方法返回给定密钥的
KeyValuePair
。下面是一个扩展方法:

<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

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

这将返回dict项的值。。。我需要整个项目作为k/v,并将dict1的项目添加到dict2!!!谢谢你的回答,我不知道LINQ方法。无论如何,我只是实现了一个字典扩展,所以我简化了k/v-pair的内联调用(因为这是在一个循环中…@dba我相信简化
单个
是没有用的,我知道你想去掉该循环中的lambda和闭包。。。但这只是为了适应它!在扩展中没有使用“Single”。而是为每一个测试和测试的关键。阿法克·兰博达斯在基地也在做同样的工作。。。可能是错的:)谢谢你的回答@dba以及lambda只是表达式。但是如果你指出LINQ只是一个漂亮的语法糖后面的一堆feachs,你是对的:D你也可以使用
SingleOrDefault
,因此如果键不存在,它将返回null或字典值类型的默认值。嗨,我知道,如何解决,问题是,如果对此有内置方法:)无论如何谢谢你@dba:不,您可以使用dic1.FirstOrDefault(Function(kv)kv.Key=“Key1”)
,但这是非常缓慢和毫无意义的。我已经在答案中添加了这一点,尽管我强烈建议您再次使用。@dba:问题是,这不是一个解决办法,因为您已经有了密钥,并且您只是在寻找值。因此,没有任何内置方法支持
O(1)
查找性能,并通过给定的键返回
KeyValuePair
。问题是,我不是在查找值,而是通过键将项作为KeyValuePair:)我希望避免动态定义新的KeyValuePair。。。这是主要的一点。。。通过定义临时变量并用信息填充它们来“匿名”添加它们对我来说是一个解决办法:)@dba:因为
KeyValuePair(TKey,TValue)
是一种值类型(
Structure
),即使使用
dict2.add,您总是在一种方式创建新对象(
Object.ReferenceEquals
总是返回false)(0,dict1.First())
。旁注:排序字典也是;-)是否要从第一个字典生成第二个字典,其中新字典的键是第一个字典的索引,值是它的KeyValuePair?否,新字典的键来自diff source。无论如何,我更新了我的问题以澄清整个任务:)Generall只是想知道我不能直接访问dict项。。。
Dim keyVal = dict1.FirstOrDefault(Function(kv)kv.Key = "Key1")
<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
Dim keyVal As KeyValuePair(Of String, Double)? = dict1.TryGetKeyValue("Key1")
If keyVal.HasValue Then dict2.Add(0, keyVal.Value)