0

我有两本字典

一种是嵌套字典——

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

普通字典的所有等值键都应该属于嵌套字典的一个键。

请建议。

4

5 回答 5

1
ObjDict.GroupBy(kvp => kvp.Value).ToDictionary(g => g.Key, g => g.Select(kvp => kvp.Key).ToList())
于 2013-07-25T07:57:58.973 回答
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
于 2013-07-25T07:59:41.147 回答
0

一些伪代码,所以你可以自己做一些工作:P

步骤1:

 make a hashTable or something which stores your ObjDict.Values and your Dict.Keys

(“0000000047510D9744C9A54EB11C0”->“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
于 2013-07-25T08:22:08.537 回答
0

您可以在Multipledictionary中创建自己的Dictionary,MultiDictionary,在添加值时维护带有键和List的私有字典,检查键是否已经存在,如果存在,添加到列表中,您可以将其设为通用

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

这就是想法,尝试自己实现其余的

于 2013-07-25T07:55:31.307 回答
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 });
    }
  }
于 2013-07-25T07:52:07.793 回答