2011-06-10 38 views
3

我有一个Dictionary与一些简单的string,string值对。问题是,有时键必须是空的多个项目,这给出了冠词错误 - >Dictonary没有独特的钥匙

 this key already exists.

是否有另一个类呢?

而且,我使用.NET 2.0,所以我不能使用的元组类...

 while (nav.MoveToNext()) 
     { 
      if (nav != null) 
      { 
       if (!String.IsNullOrEmpty(nav.Value)) 
       { 
        if (nav.HasChildren) 
        { 
         navChildren = nav.Clone(); 
         navChildren.MoveToFirstChild(); 
         if (navChildren != null) 
          if (!veldenToSkip.Contains(nav.LocalName.Trim().ToLower()) 
           && !nav.LocalName.StartsWith("opmerkingen_")) 
           itemTable.Add(nav.LocalName.Replace("_", " "), navChildren.Value); 
           //normal key and value 
         while (navChildren.MoveToNext()) 
         { 
          if (!veldenToSkip.Contains(nav.LocalName.Trim().ToLower())) 
          { 
           if (navChildren != null) 
           { 
            if (!String.IsNullOrEmpty(navChildren.Value)) 
            { 
             itemTable.Add("", navChildren.Value); 
             //Add blank keys 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 

我只希望这样的结构:

value1 value2 
value3 value4 
      value5 
      value6 
value7 value8 
... 
+0

见http://stackoverflow.com/questions/1171812/multi -key词典式-C。 – aligray 2011-06-10 11:08:44

+1

为什么你需要一个没有密钥的字典中的值对?你以后怎么找到它?考虑不添加值对,直到你有一把钥匙或翻开字典(切换键和值)。 – InBetween 2011-06-10 11:09:21

+0

@aligray我不认为这是相同的,他希望允许多个条目为同一个键,而不是每个条目多个键。 – 2011-06-10 11:09:35

回答

2

就产生一个伪键...

int emptyKey = 0; 

... 
if (!String.IsNullOrEmpty(navChildren.Value)) 
{ 
    string key = "Empty_" + emptyKey.ToString(); 
    emptyKey ++; 
    itemTable.Add(key, navChildren.Value); 
    //Add blank keys 
} 

你还有价值,但注意,字典不保存订单(添加)。

+0

mmmmyeah这将工作太:-)谢谢 – 2011-06-10 11:47:20

1

尝试使用元组:

http://sankarsan.wordpress.com/2009/11/29/tuple-in-c-4-0/

更新

好吧现在帖子说.Net 2.0所以...这个答案不能工作!

我想这可能是有用的:

Dictionary w/ null key?

+0

我不认为适用,他想允许同一个键的多个条目,而不是每个条目的多个键 – 2011-06-10 11:11:52

+0

该帖子告诉他想要使用字典字符串,有时使用null键的字符串。我认为Tuple可以工作。 – danyolgiax 2011-06-10 11:15:49

+0

更新了帖子! – danyolgiax 2011-06-10 11:20:54

6

你可以实现的ILookup接口...

裹字典< TKEY的,名单< TValue>中>

+0

谢谢,但ILookup接口似乎只能从.NET 3.5获得,我正在使用.NET 2.0 .... – 2011-06-10 11:36:13

1

虽然很详细,这可以工作:

class Pair<A, B> 
{ 
    public A Key { get; set; } 
    public B Value{ get; set; } 
} 

var items = new List<Pair<string, string>>(); 

items.Add(new Pair<string,string>() { Key = "", Value = "Test" }); 
items.Add(new Pair<string,string>() { Key = "", Value = "Test" }); 
+1

如何查找列表? – 2011-06-10 11:21:36

+0

是的,只是试过了! – aligray 2011-06-10 11:23:13

3

你可以使用一个Dictionary<yourKeyType, List<yourObjectType>>.就像你可以为每个键添加多个项目...在添加之前,检查一个List是否已经存在用于你的键 - >添加它,否则创建一个新的List。更优雅的包装它在内部处理它的类。一类

例如,你可以使用:

class MultiValueDictionary<TKey, TValue> 
{ 
    private Dictionary<TKey, List<TValue>> _InternalDict = new Dictionary<TKey, List<TValue>>(); 

    public void Add(TKey key, TValue value) 
    { 
     if (this._InternalDict.ContainsKey(key)) 
      this._InternalDict[key].Add(value); 
     else 
      this._InternalDict.Add(key, new List<TValue>(new TValue[]{value})); 
    } 

    public List<TValue> GetValues(TKey key) 
    { 
     if (this._InternalDict.ContainsKey(key)) 
      return this._InternalDict[key]; 
     else 
      return null; 
    } 

} 
3

由于具有相同的密钥多个值排序的否定词典的实用性,KeyValuePairs的名单经常更有意义:

List<KeyValuePair<string, string>> itemTable = new List<KeyValuePair<string, string>>();