Category Archives: Dictionary

Operation On Dictionary

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:

AttributeChange Class
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace DictionaryCollection
  6. {
  7.     public class Attributes
  8.     {
  9.         public Attributes( string fieldName, string attributeName,
  10.             string attributeValue, bool overwrite)
  11.         {
  12.             this.FiledName = fieldName;
  13.             this.AttributeName = attributeName;
  14.             this.AttributeValue = attributeValue;
  15.             this.Overwrite = overwrite;
  16.         }
  17.         public string FiledName
  18.         {
  19.             get { return _fieldName; }
  20.             set { _fieldName = value; }
  21.         }
  22.         private string _fieldName;
  23.  
  24.         public string AttributeName
  25.         {
  26.             get { return _attributeName; }
  27.             set { _attributeName = value; }
  28.         }
  29.         private string _attributeName;
  30.  
  31.         public string AttributeValue
  32.         {
  33.             get { return _attributeValue; }
  34.             set { _attributeValue = value; }
  35.         }
  36.         private string _attributeValue;
  37.  
  38.         public bool Overwrite
  39.         {
  40.             get { return _overwrite; }
  41.             set { _overwrite = value; }
  42.         }
  43.         private bool _overwrite;
  44.  
  45.     }
  46. }

 

Here is the Code for Dictionary to Update it :

CreateDictionary
  1. private static void CreateDictionary(List<Attributes> attributeList)
  2.         {            
  3.             foreach (Attributes attribute in attributeList)
  4.             {
  5.                 List<string> valuesListForThisKey = new List<string>();
  6.                 //Check if the Key(attributeName) is already there
  7.                 if (customSettingDictionary.ContainsKey(attribute.AttributeName))
  8.                 {
  9.                     customSettingDictionary.TryGetValue(attribute.AttributeName, out valuesListForThisKey);
  10.                     //Check wheter Overwrite is true
  11.                     if (attribute.Overwrite == true)
  12.                     {                      
  13.                         //Clear Previous values and add new value
  14.                         valuesListForThisKey.Clear();
  15.                         valuesListForThisKey.Add(attribute.AttributeValue);
  16.                         customSettingDictionary.Remove(attribute.AttributeName);
  17.                         customSettingDictionary.Add(attribute.AttributeName, valuesListForThisKey);
  18.                     }
  19.                     //If overwrite is flase
  20.                     else
  21.                     {
  22.                         //Check if the Value is doesn’t alredy associate with the same key
  23.                         if (!valuesListForThisKey.Contains(attribute.AttributeValue))
  24.                         {
  25.                             valuesListForThisKey.Add(attribute.AttributeValue);
  26.                             customSettingDictionary.Remove(attribute.AttributeName);
  27.                             customSettingDictionary.Add(attribute.AttributeName, valuesListForThisKey);
  28.                         }
  29.                     }
  30.                 }
  31.                 //If the key doesn’t exits alrady, add a new entry
  32.                 else
  33.                 {
  34.                     valuesListForThisKey.Add(attribute.AttributeValue);
  35.                     customSettingDictionary.Add(attribute.AttributeName, valuesListForThisKey);
  36.                 }
  37.             }
  38.         }

 

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:

Code Snippet
  1. foreach (KeyValuePair<string, List<string>> kvp in customSettingDictionary)
  2.             {
  3.                 Console.WriteLine(kvp.Key);
  4.                 foreach (string value in kvp.Value)
  5.                 {
  6.                     Console.WriteLine(“\t” + value);
  7.                 }
  8.             }

 

Input:

Code Snippet
  1. List<Attributes> attributeList = new List<Attributes>();
  2.             attributeList.Add(new Attributes(“__CustomSetting”,“MyName”,“Altaf”,true));
  3.             attributeList.Add(new Attributes(“__CustomSetting”, “MyName”, “altaf”, false));
  4.             attributeList.Add(new Attributes(“__CustomSetting”, “MyName”, “altaf”, false));
  5.             attributeList.Add(new Attributes(“__CustomSetting”, “MySetting”, “set”, true));
  6.             attributeList.Add(new Attributes(“__CustomSetting”, “MyName”, “altaf”, true));
  7.             attributeList.Add(new Attributes(“__CustomSetting”, “MyName”, “altaf23″, false));
  8.             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.

image

Follow

Get every new post delivered to your Inbox.

Join 54 other followers