应用字符串。加入词典<string, List<int>>

问题描述 投票:0回答:1

我遇到的情况是我有一本类型为

Dictionary<string, List<int>>
的字典。我想应用 sting.Join 到它,我希望数据显示如下:

"abc1:1,2,3,4,abc2:1,3,8,abc3:3,1,7" 

我尝试的是这样的:

$"{string.Join(", ", iformation.OrderBy(x => x.Key).Select(x => x.Key + ": " + string.Join(",", x.Value.ToArray())))}";

输出是:

System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Collections.Generic.List`1[System.Int32]]:

有人可以帮助我实现我想要实现的目标吗?

c# string dictionary join .net-5
1个回答
-1
投票

试试这个代码:

var dict = new Dictionary<string, List<int>>();

var result = string.Join(
    ", ", 
    dict.Select(x => 
        $"{x.Key}: [{string.Join(",", x.Value)}]"));
© www.soinside.com 2019 - 2024. All rights reserved.