10

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

GoldBishop
  • 2,820
  • 4
  • 47
  • 82
  • I would have written Linq like this (sorry for the C#-vb.net soup...) var ret = String.Join(",",Me._set.Select(kvp=>String.Format("{0}={1}",kvp.Key, kvp.Value).ToArray()); – jbl Nov 28 '12 at 16:58
  • 1
    put that as an answer....and i do enough C# development to understand it. Just more comfortable with VB.Net. – GoldBishop Nov 28 '12 at 17:00
  • The .net gurus seem to prefer vb.net over C#. They can't be wrong. I think I'm just the turbo-pascal generation, with semi-colons and curly braces ;-) – jbl Nov 28 '12 at 17:16

3 Answers3

10

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,

jbl
  • 15,179
  • 3
  • 34
  • 101
  • 1
    worked like a charm once i translated to VB.Net. Conversion equivalent: `String.Join(", ", Me.Select(Function(kvp) String.Format("{0}={1}", kvp.Key, kvp.Value).ToArray()))` – GoldBishop Nov 28 '12 at 18:23
  • `_set` is private variable which is the `Dictionary(Of String, String)` – GoldBishop Nov 28 '12 at 18:45
  • Changed it so the class was extended from `Dictionary(Of String, STring)` as it made operating against the collection easier. – GoldBishop Nov 28 '12 at 18:47
  • 2
    I found a parenthesis error in vb.net. It should be `return String.Join(", ", Me.Select(Function(kvp) String.Format("{0}={1}", kvp.Key, kvp.Value)).ToArray())` – Matt Roy Feb 06 '18 at 15:17
  • the parenthesis error caused it to instead print as System.char[] – cheft Jun 26 '19 at 15:51
6

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
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
3

VB.net syntax:

Dim dic As New Dictionary(Of String, String)() From {
            {"a", "1"},
            {"b", "2"},
            {"c", "3"},
            {"d", "4"}
            }

Dim s As String = String.Join(",", dic.Select(Function(pair) String.Format("{0}={1}", pair.Key, pair.Value)).ToArray())
Steve
  • 20,703
  • 5
  • 41
  • 67
  • Same exact code as @jbl posted above. Please add to the thread dont just put a brighter shine on something already suggested. – GoldBishop Nov 29 '12 at 14:19
  • 1
    When i posted this there was only c# syntax in jbl's answer, i am surprised you think that is somehow worth a downvote, but i can live with it! – Steve Nov 29 '12 at 15:10
  • 1
    @GoldBishop That is not how SO works. People edit their answers and it may not even be apparent that they have. You will find multiple similar answers. There is prbably little or no time difference. On meta it mentions that downvotes should be on correctness of an answer rtaken in isolarion. – Fionnuala Dec 01 '12 at 11:03
  • `dic.select` didn't work for me. Perhaps because I'm declaring it as a general `object` – Bernardo Dal Corno Feb 01 '18 at 17:50