Category Archives: Dictionary
Operation On Dictionary
Posted by Altaf
In this post i will show how we can set value in a Dictionary<string, List<string>> and also retrieve from it:
What we want to add:
Dictionary add values as key value pair. So, we want to achieve a dictionary like:
| Key1 | Value |
| Altaf | Radio |
| desktop | |
| Laptop |
| Key2 | Value |
| User2 | Radio2 |
| desktop2 | |
| Laptop |
Here is a simple method we can set values to the:
Here I am adding an attribute change object properties to Dictionary:
Which is like:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace DictionaryCollection
- {
- public class Attributes
- {
- public Attributes( string fieldName, string attributeName,
- string attributeValue, bool overwrite)
- {
- this.FiledName = fieldName;
- this.AttributeName = attributeName;
- this.AttributeValue = attributeValue;
- this.Overwrite = overwrite;
- }
- public string FiledName
- {
- get { return _fieldName; }
- set { _fieldName = value; }
- }
- private string _fieldName;
- public string AttributeName
- {
- get { return _attributeName; }
- set { _attributeName = value; }
- }
- private string _attributeName;
- public string AttributeValue
- {
- get { return _attributeValue; }
- set { _attributeValue = value; }
- }
- private string _attributeValue;
- public bool Overwrite
- {
- get { return _overwrite; }
- set { _overwrite = value; }
- }
- private bool _overwrite;
- }
- }
Here is the Code for Dictionary to Update it :
- private static void CreateDictionary(List<Attributes> attributeList)
- {
- foreach (Attributes attribute in attributeList)
- {
- List<string> valuesListForThisKey = new List<string>();
- //Check if the Key(attributeName) is already there
- if (customSettingDictionary.ContainsKey(attribute.AttributeName))
- {
- customSettingDictionary.TryGetValue(attribute.AttributeName, out valuesListForThisKey);
- //Check wheter Overwrite is true
- if (attribute.Overwrite == true)
- {
- //Clear Previous values and add new value
- valuesListForThisKey.Clear();
- valuesListForThisKey.Add(attribute.AttributeValue);
- customSettingDictionary.Remove(attribute.AttributeName);
- customSettingDictionary.Add(attribute.AttributeName, valuesListForThisKey);
- }
- //If overwrite is flase
- else
- {
- //Check if the Value is doesn’t alredy associate with the same key
- if (!valuesListForThisKey.Contains(attribute.AttributeValue))
- {
- valuesListForThisKey.Add(attribute.AttributeValue);
- customSettingDictionary.Remove(attribute.AttributeName);
- customSettingDictionary.Add(attribute.AttributeName, valuesListForThisKey);
- }
- }
- }
- //If the key doesn’t exits alrady, add a new entry
- else
- {
- valuesListForThisKey.Add(attribute.AttributeValue);
- customSettingDictionary.Add(attribute.AttributeName, valuesListForThisKey);
- }
- }
- }
Here when adding I am checking whether the Key is there then also trying to get the value:
We can do this in a single operation in
if(customSettingDictionary.TryGetValues( currentKey, out valuesListForCurrentKey))
{
………………….
}
To Retrieve Data we can use KeyValuePair thats fits with the Dictionary we are working on:
- foreach (KeyValuePair<string, List<string>> kvp in customSettingDictionary)
- {
- Console.WriteLine(kvp.Key);
- foreach (string value in kvp.Value)
- {
- Console.WriteLine(“\t” + value);
- }
- }
Input:
- List<Attributes> attributeList = new List<Attributes>();
- attributeList.Add(new Attributes(“__CustomSetting”,“MyName”,“Altaf”,true));
- attributeList.Add(new Attributes(“__CustomSetting”, “MyName”, “altaf”, false));
- attributeList.Add(new Attributes(“__CustomSetting”, “MyName”, “altaf”, false));
- attributeList.Add(new Attributes(“__CustomSetting”, “MySetting”, “set”, true));
- attributeList.Add(new Attributes(“__CustomSetting”, “MyName”, “altaf”, true));
- attributeList.Add(new Attributes(“__CustomSetting”, “MyName”, “altaf23″, false));
- attributeList.Add(new Attributes(“__CustomSetting”, “Myname”, “altaf23″, false));
OUT PUT:
Things to be noted here that if Attribute.Overwrite true then it overwrite the previous list and add it self.