我有两本词典
一个是嵌套字典 -
Dictionary<string, List<string>> Dict = new Dictionary<string, List<string>>();
另一个是正常的 -
Dictionary<string, string> ObjDict = new Dictionary<string, string>();
在普通字典中我有这样的值
{[DateTime_7_25_2013_12_26_11_PM_Table_2_1_Trade_2_1.xml,0000000047510D9744C9A54EB11C0]} {[DateTime_7_25_2013_12_26_11_PM_Table_2_1_Trade_2_2.xml,0000000047510D9744C9A54EB11C0]} {[DateTime_7_25_2013_12_26_11_PM_Table_2_2_Trade_3_1.xml,0000000047510D9744C9A54EB11C1]} {[DateTime_7_25_2013_12_26_11_PM_Table_2_2_Trade_3_2.xml,0000000047510D9744C9A54EB11C1]} {[DateTime_7_25_2013_12_26_11_PM_Table_2_2_Trade_3_3.xml,0000000047510D9744C9A54EB11C2]}
现在我想要这样的嵌套字典 -
“Key0” DateTime_7_25_2013_12_26_11_PM_Table_2_1_Trade_2_1.xml
DateTime_7_25_2013_12_26_11_PM_Table_2_1_Trade_2_2.xml
“Key1” DateTime_7_25_2013_12_26_11_PM_Table_2_2_Trade_3_1.xml
DateTime_7_25_2013_12_26_11_PM_Table_2_2_Trade_3_2.xml
“Key2” DateTime_7_25_2013_12_26_11_PM_Table_2_2_Trade_3_3.xml
普通字典的相等值的所有键应该属于嵌套字典的一个键。
请建议。
答案 0 :(得分:1)
ObjDict.GroupBy(kvp => kvp.Value).ToDictionary(g => g.Key, g => g.Select(kvp => kvp.Key).ToList())
答案 1 :(得分:1)
好的,如果我正确理解了这个问题:
假设您有一个输入字典,您希望按字典中的值对项目进行分组,并将它们放入一个新的字典中,其中每个键是原始字典中的一个值,每个值都是一个列表。具有该值的键:
var items = new Dictionary<string, string>
{
{"C03", "C"},
{"B01", "B"},
{"A01", "A"},
{"C02", "C"},
{"A03", "A"},
{"B03", "B"},
{"B02", "B"},
{"A02", "A"},
{"C01", "C"}
};
var result = items.GroupBy(item => item.Value)
.ToDictionary
(
g => g.Key,
g => g.Select(x => x.Key).ToList()
);
foreach (var item in result)
{
Console.Write("Values for key " + item.Key + ": ");
foreach (var value in item.Value)
Console.Write(value + " ");
Console.WriteLine();
}
上面的代码产生以下输出:
Values for key C: C03 C02 C01
Values for key B: B01 B03 B02
Values for key A: A01 A03 A02
如您所见,此输出未被排序(它与输入的顺序相同)。
如果您想订购字典中的两个键以及每个列表中的值,您可以这样做:
var result = items.GroupBy(item => item.Value)
.OrderBy(item => item.Key)
.ToDictionary
(
g => g.Key,
g => g.Select(x => x.Key).OrderBy(x => x).ToList()
);
此更改产生此输出:
Values for key A: A01 A02 A03
Values for key B: B01 B02 B03
Values for key C: C01 C02 C03
答案 2 :(得分:0)
你可以使用它:
foreach (var item in ObjDict)
{
if (Dict.ContainsKey(item.Value))
{
var e = Dict[item.Value];
e.Add(item.Key);
}
else
{
Dict.Add(item.Value, new List<string>() { item.Key });
}
}
答案 3 :(得分:0)
你可以创建自己的Dictionary,MultiDictionary, 在Multipledictionary中,使用key和List维护一个私有字典 添加值时,检查密钥是否已存在,如果存在,则添加到列表中 你可以把它变成Generic
public class MultipleDictionary<TKey, TValue>
{
private IDictionary<TKey, IList<TValue>> _dict;
public bool ContainsKey(TKey key)
{
return _dict.ContainsKey(key);
}
public TValue this[TKey key]
{
get
{
if (_dict.ContainsKey(key))
return _dict[key][0];
return default(TValue);
}
set
{
if (_dict.ContainsKey(key))
{
IList<TValue> valList = _dict[key];
valList.Add(value);
}
else
{
IList<TValue> list = new List<TValue>();
list.Add(value);
_dict.Add(key, list);
}
}
}
}
这是想法,试着自己实施其余的
答案 4 :(得分:0)
有点伪代码,所以你可以自己做一些工作:P
第1步:
make a hashTable or something which stores your ObjDict.Values and your Dict.Keys
(“0000000047510D9744C9A54EB11C0” - &gt;“Key0”)
步骤2:
for each ObjDict.Key
if(Dict.contains(HashTable[thisKey])
Take List from Dict and add the ObjDict.Value
else
Make new list, add ObjDict.Value
Dict.Add(HashTable[thiskey], theList