vb.net 将 Dictionary 转换为结构化格式字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13610071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:21:34  来源:igfitidea点击:

Convert Dictionary into structured format string

vb.netcollections.net-4.0generic-collections

提问by GoldBishop

I have a Dictionary object declared as var as Dictionary(of String, String).

我有一个 Dictionary 对象声明为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.

我正在尝试利用通用集合可用的 LINQ 扩展,但我只获得非扩展方法。

I need to turn the Dictionary collection into a string with the following pattern: key1=val1, key2=val2, ..., keyn=valn

我需要将 Dictionary 集合转换为具有以下模式的字符串: 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.

起初以为做一个 foreach 循环会适得其反,除了我是程序员的事实。

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.Linqlibraries 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.

出于某种原因,即使我已将System.Core&System.Linq库导入到项目中,我的 dev-env 智能感知中也没有显示任何扩展的 LINQ 扩展。所以现在,除非有人可以帮助我让 LINQ 扩展出现在 Intellisense 中,否则它们是不可能的。

Found the problem with the LINQ extensions not showing up, so they are back on the table ;)

发现 LINQ 扩展没有出现的问题,所以它们又回到了桌面上 ;)

回答by jbl

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

我会像这样用 Linq 编写整个方法块(对不起 C#-vb.net 汤...)

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 :

另外,我真的不知道 _set 是什么。也许你必须投射:

c-sharp:

c-锋利:

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

vb.net:

vb.net:

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

Hope this will help,

希望这会有所帮助,

回答by Steven Doggart

As far as your non-LINQ loop goes, I would recommend doing it like this:

就您的非 LINQ 循环而言,我建议这样做:

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:

使用 LINQ,你可以这样做:

Public Overrides Function ToString() As String
    Return String.Join(", ", _set.Select(Function(pair) $"{pair.Key}={pair.Value}"))
End Function

回答by Steve

VB.net syntax:

VB.net 语法:

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