Dictonar without unique keys

I have Dictionarywith some simple pairs of values string,string. The problem is that sometimes the key must be empty for several elements, which gives an error in the request →

 this key already exists.

Is there any other class for this?

Also, I am using .NET 2.0, so I cannot use the Tuple class ...

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

I only need a structure:

value1    value2
value3    value4
          value5
          value6
value7    value8
...
+3
source share
6 answers

Just generate a pseudo-key ...

int emptyKey = 0;

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

You still have values, but note that the dictionary does not preserve order (additions).

+2
source

you can implement the iLookup interface ...

wrap dictionary <TKey, List <TValue →

+6
source

Dictionary<yourKeyType, List<yourObjectType>>. , ... , → , . , , .

, :

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>>();
+3

Although this is very verbose, this might work:

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
source

All Articles