Home > Net >  Map two lists into a dictionary(of object, object) in VB.NET
Map two lists into a dictionary(of object, object) in VB.NET

Time:01-27

given two lists of the same length, I need to zip the two of them together into a dictionary. I know this can be done using looping but didn't particularly want to go that route. 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.

After reviewing the MSDN on iEnumerable.Zip and the referenced solution above, I have the partial solution below. I am struggling implementing the .toDictionary portion. Sample input for each list and current code is below along with current output and desired output.

I am working in a legacy system 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"]
}

CodePudding user response:

Note that the C# version you linked to has two =>s, so:

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