简体   繁体   English

Map 两个列表在 VB.NET 中的字典(object,对象)

[英]Map two lists into a dictionary(of object, object) in VB.NET

given two lists of the same length, I need to zip the two of them together into a dictionary.给定两个长度相同的列表,我需要将 zip 将它们两个一起放入字典中。 I know this can be done using looping but didn't particularly want to go that route.我知道这可以使用循环来完成,但并不是特别想要 go 这条路线。 I've seen a few other solutions Map two lists into a dictionary in c# but nothing in VB and I'm having a bit of trouble porting it to VB.NET.我在 c# 中看到了一些其他解决方案Map 两个列表到字典中,但在 VB 中什么也没有,我在将它移植到 VB.NET 时遇到了一些麻烦。

After reviewing the MSDN on iEnumerable.Zip and the referenced solution above, I have the partial solution below.在查看iEnumerable.Zip 上的 MSDN 和上面引用的解决方案之后,我有下面的部分解决方案。 I am struggling implementing the .toDictionary portion.我正在努力实施.toDictionary部分。 Sample input for each list and current code is below along with current output and desired output.下面是每个列表和当前代码的示例输入以及当前 output 和所需的 output。

I am working in a legacy system targeting.Net 4.0我在遗留系统中工作 targeting.Net 4.0

Public Shared sub Main()
    Dim headers As Object() = {"key1", "key2", "key3"}
    Dim recordData As Object() = {"value1", 2, "value3"}

    Dim ziperator = headers.Zip(recordData, 
        Function(h, r) New With {h, r}).ToDictionary(Function(x) x.r)

End Sub
'Current output:
ziperator = {
    ["key1",{"key1","value1"}], 
    ["key2",{"key2",2}],
    ["key3",{"key3","value3"}]
}

'Desired output:
ziperator = {
    ["key1","value1"],
    ["key2", 2],
    ["key3","value3"]
}

Note that the C# version you linked to has two => s, so:请注意,您链接到的 C# 版本有两个=> ,因此:

Dim ziperator = headers.Zip(recordData,
                            Function(h, r) New With {h, r}).
                            ToDictionary(Function(q) q.h, Function(q) q.r)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM