Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary if Key exist append if not add new element C#

I am having

Dictionary<String, List<String>> filters = new Dictionary<String, List<String>>();

which is having values like country = us. till now I am able to add it when key is not repeated. now when key country is repeated. it is showing that the key is already present.

what I want is How to add multiple values in the same key. I am not able to do it. Please suggest something.

for (int i = 0; i < msgProperty.Value.Count; i++)
{
    FilterValue.Add(msgProperty.Value[i].filterValue.Value);
    filterColumn = msgProperty.Value[i].filterColumnName.Value;
    filters.Add(filterColumn, FilterValue);
}

what I want

country = US,UK

like image 469
Ameya Deshpande Avatar asked Sep 07 '15 13:09

Ameya Deshpande


People also ask

Which method will add a key to the dictionary if it does not already exist in the dictionary?

Method 1: Add new keys using the Subscript notation This method will create a new key\value pair on a dictionary by assigning a value to that key. If the key doesn't exist, it will be added and will point to that value.

How do you check if a given key already exists in a dictionary?

Using has_key() method returns true if a given key is available in the dictionary, otherwise, it returns a false. With the Inbuilt method has_key(), use the if statement to check if the key is present in the dictionary or not.

Which of the following method will add a key to the dictionary if it does not already exist in 1 the dictionary it should not update the content otherwise?

Which method will add a key to the dictionary if it does not already exist in the dictionary? The Python dictionary setdefault() method returns the value of a key if it already exists in the dictionary. If it does not exist, the key-value pair gets inserted into the dictionary.

What method could I use to see if a key exists in a dictionary and if it does not will set a key's value answer in the form of method ()?

Checking if key exists using the get() method The get() method is a dictionary method that returns the value of the associated key. If the key is not present it returns either a default value (if passed) or it returns None. Using this method we can pass a key and check if a key exists in the python dictionary.


1 Answers

The different types of all your variables are a bit confusing, which won't help you writing the code. I'm assuming you have a Dictionary<string, List<string>> where the key is a "language" and the value is a list of countries for that language, or whatever. Reducing a problem to a minimal set that reproduces the issue is very helpful when asking for help.

Anyway assuming the above, it's as simple as this:

  • Try to get the dictionary["somelanguage"] key into existingValue.
  • If it doesn't exist, add it and store it in the same variable.
  • Add the List<string> to the dictionary under the "somelanguage" key.

The code will look like this:

private Dictionary<string, List<string>> dictionary;

void AddCountries(string languageKey, List<string> coutriesToAdd)
{
    List<string> existingValue = null;

    if (!dictionary.TryGetValue(languageKey, out existingValue))
    {
        // Create if not exists in dictionary
        existingValue = dictionary[languageKey] = new List<string>()
    }

    existingValue.AddRange(coutriesToAdd);
}
like image 165
CodeCaster Avatar answered Sep 17 '22 19:09

CodeCaster