I have a Dictionary object declared as var as Dictionary(of String, String)
.
I am trying to utilize the LINQ extensions available to the Generic Collection but am only getting the non-extension methods.
I need to turn the Dictionary collection into a string with the following pattern: key1=val1, key2=val2, ..., keyn=valn
Thought at first doing a foreach loop would hit the spot except the fact that i am programmers-block.
What i have so far, but doubt its the best logic pattern for producing this:
Public Overrides Function ToString() As String
Dim ret As String = ""
For Each kv As KeyValuePair(Of String, String) In Me._set
If ret <> String.Empty Then
ret &= ", "
End If
ret &= String.Format("{0}={1}", kv.Key, kv.Value)
Next
Return ret
End Function
And for some reason even though i have imported the System.Core
& System.Linq
libraries into the project none of the extended LINQ extensions are showing up in my dev-env intellisense. So for now, unless someone could help me get the LINQ extensions to show up in Intellisense, they are out of the question.
Found the problem with the LINQ extensions not showing up, so they are back on the table ;)
You can easily convert a Python dictionary to a string using the str() function. The str() function takes an object (since everything in Python is an object) as an input parameter and returns a string variant of that object.
eval() is an inbuilt python library function used to convert string to dictionary efficiently. For this approach, you have to import the ast package from the python library and then use it with the literal_eval() method.
Dictionary to List Using .items() method of the dictionary is used to iterate through all the key-value pairs of the dictionary. By default, they are stored as a tuple (key, value) in the iterator. Using list() will get you all the items converted to a list.
As far as your non-LINQ loop goes, I would recommend doing it like this:
Public Overrides Function ToString() As String
Dim items As New List(Of String)(_set.Count)
For Each pair As KeyValuePair(Of String, String) In _set
items.Add($"{pair.Key}={pair.Value}"))
Next
Return String.Join(", ", items)
End Function
With LINQ, you could do it like this:
Public Overrides Function ToString() As String
Return String.Join(", ", _set.Select(Function(pair) $"{pair.Key}={pair.Value}"))
End Function
I would have written the whole method block with Linq like this (sorry for the C#-vb.net soup...)
return String.Join(",",Me._set.Select(kvp=>String.Format("{0}={1}",kvp.Key, kvp.Value).ToArray());
Also, I don't really know what _set is. Maybe you'll have to cast :
return String.Join(",", Me._set.Cast<KeyValuePair<String,String>>().Select(kvp=>String.Format("{0}={1}",kvp.Key, kvp.Value).ToArray());
return String.Join(", ", Me.Select(Function(kvp) String.Format("{0}={1}", kvp.Key, kvp.Value)).ToArray())
Hope this will help,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With