-2

がある

Dictionary<string,List<string>> dictionary=new Dictionary<string,List<string>>();

var c;

var c returns me values : 
100, "somestring"
100,"someotherstring"
200,"two"

foreach(var d in c)
{
 dict.Add(d.key,d.value);
// Need to add key value pairs here for dictionary. if key is same then values should get concatenated.
}

foreach(keyvaluepair<string,List<string>> pair in dictionary)
{
    // This loop should output something like below...
       100,"somestring,someotherstring"
       200,"two"

}
4

4 に答える 4

1

key value不可能、この目的のためにフィールドを持つ独自のクラスを作成する

于 2012-07-20T11:28:23.727 に答える
1

辞書に重複キーを含めることはできないため、コード行はdict.Add(d.key,d.value);例外をスローします

于 2012-07-20T11:26:18.620 に答える
0

値を追加するときは、キーが存在するかどうかを確認する必要があります。

if (!dict.ContainsKey(d.key)) {
  dict.Add(d.key, new List<string>());
}
dict[d.Key].Add(d.Value);

値を出力するときは、文字列を結合します。

Console.Write(pair.Key + ", " + String.Join(", ", pair.Value));
于 2012-07-20T11:43:11.507 に答える
0

as value で辞書を作成しList<string>、値を追加するだけです:

foreach(var d in c)
{
  if (!dict.ContainsKey(d.Key))
    dict.Add(d.Key, new List<string>());
  dict[d.Key].Add(d.Value);
}

その後、リストからカンマ区切りの文字列を取得できますstring.Join

string commaDelimitedList = string.Join(",", valueList.ToArray());
于 2012-07-20T11:36:30.670 に答える