没有唯一关键字的词典

本文关键字:关键字 唯一 | 更新日期: 2023-09-27 17:58:47

我有一个Dictionary和一些简单的string,string值对。问题是,有时多个项的键必须为空,这会导致字典错误->

此键已经存在

这个还有别的课吗?

另外,我使用的是.NET 2.0,所以我不能使用Tuple类。。。

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
...

没有唯一关键字的词典

您可以实现ILookup接口。。。

包装字典<TKey,列表<TValue>>

因为拥有多个具有相同关键字的值会否定字典的效用,因此KeyValuePairs列表通常更有意义:

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

您可以使用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;
    }
}

只生成一个伪密钥。。。

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

您仍然会有Values,但请注意Dictionary不会保留(添加)顺序。

尝试使用Tuple:

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

更新

好吧,现在帖子上写着.Net 2.0,所以……这个答案不起作用!

我认为这是有用的:

字典是否有空键?

虽然非常详细,但这可以工作:

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