Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary without unique keys

Tags:

c#

.net

generics

I have a Dictionary with some simple string,string value pairs. The problem is that sometimes the key has to be empty for multiple items, which gives a dictionary error ->

 this key already exists.

Is there another class for this?

Also, I'm using .NET 2.0 so I can't 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 want a structure like this:

value1    value2
value3    value4
          value5
          value6
value7    value8
...
like image 559
Run CMD Avatar asked Dec 04 '22 08:12

Run CMD


2 Answers

you could implement the ILookup interface ...

wrap a Dictionary< TKey,List< TValue > >

like image 193
DarkSquirrel42 Avatar answered Dec 19 '22 06:12

DarkSquirrel42


Because having multiple values with the same key sort of negates the utility of a dictionary, a List of KeyValuePairs frequently makes more sense:

List<KeyValuePair<string, string>> itemTable = new List<KeyValuePair<string, string>>();
like image 22
xr280xr Avatar answered Dec 19 '22 05:12

xr280xr