Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Dictionary into structured format string

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 ;)

like image 260
GoldBishop Avatar asked Nov 28 '12 16:11

GoldBishop


People also ask

How do you turn a dictionary into a string?

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.

How do you convert a single string to a dictionary in Python?

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.

How do I change a dictionary to a list?

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.


2 Answers

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
like image 139
Steven Doggart Avatar answered Oct 12 '22 02:10

Steven Doggart


I would have written the whole method block with Linq like this (sorry for the C#-vb.net soup...)

c-sharp

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 :

c-sharp:

return String.Join(",", Me._set.Cast<KeyValuePair<String,String>>().Select(kvp=>String.Format("{0}={1}",kvp.Key, kvp.Value).ToArray());

vb.net:

return String.Join(", ", Me.Select(Function(kvp) String.Format("{0}={1}", kvp.Key, kvp.Value)).ToArray())

Hope this will help,

like image 43
jbl Avatar answered Oct 12 '22 01:10

jbl