Home > Net >  Using large amount of data stored in a dictionary to create a new dictionary where each key value pa
Using large amount of data stored in a dictionary to create a new dictionary where each key value pa

Time:12-28

I have a large dictionary (inpData) which contains data input from a user form. I need to create a new dictionary (outData) in which the keys / values will be dependent on the inpData, logic statements and lookup tables will be required. In the future I want to include some form of validation on the inpData.

My current approach is as follows:

  1. Create a class for each sub-table of the outData dictionary.
  2. Use the constructor to perform the manipulation from the inpData table to a Property of the class.
  3. I will then create a function to combine the Properties into a dictionary for amalgamation.

Example below:


    internal class LocationData
    {
        //Properties

        private string locationType { get; set; }
        private string locationValue { get; set; }

        //Constructor

        public LocationData(Dictionary<string, object> inpData)
        {
            this.locationType = (string)inpData["locationType"];

            switch (this.locationType)
            {
                case "MECD":
                    locationValue = "High";
                    break;
                case "LECD":
                    locationValue = "Low";
                    break;
            }




        }

My question is whether this follows a best practise approach / whether there is a better way to do such a task.

CodePudding user response:

It looks good but I have a few comments.

you could have a dictionary for the mapping of (MECD and High....) so you don't require switch case here and you can use this dictionary at other places also in your project.

you can also handle the case when the item is not found in the dictionary.

I think locationValue variable name should be changed to something locationRating for good readability.

  •  Tags:  
  • c#
  • Related