简体   繁体   中英

Sum values of a Dictionary for specific keys C#

I have a dictionary with two similar keys, one is misspelled so it created another key, that I would like to sum the values of, and delete the key/value pair that is misspelled.

If I have a dictionary of departments, with the department name as the key, and the number of personnel in that department as the value, I would like to sum the values for "Marketing" and "Marketin" and delete the misspelled key/value pair "Marketin".

        var departments = new Dictionary<string, int>()
        {
            { "HR", 33 },
            { "Management", 8 },
            { "Marketing", 21 },
            { "Marketin", 4 },
            { "Sales", 44 }
        };

I would like the dictionary to look like this:

        var departments = new Dictionary<string, int>()
        {
            { "HR", 33 },
            { "Management", 8 },
            { "Marketing", 25 },
            { "Sales", 44 }
        };

The dictionary is already created, so I only used an initialization for illustration purposes.

departments["Marketing"] += departments["Marketin"];

departments.Remove("Marketin");

Here's my sketch for more "generic" method:

var normalisedDepartments = 
   departments.GroupBy(x => Normalise(x.Key), y => y.Value)
             .ToDictionary(x => x.Key, y => y.Sum());

    private static string Normalise(string key)
    {
        if (key == "Marketin")
        {
            return "Marketing";
        }
        return key;
    }

Then you can tweak Normalise method so that it recognizes other malformed keys, should there be more of them.

Here is a code sample that illustrates the Dictionary capabilities that you can use to do what you want.

As for which combination of these functions that you need to use to accomplish your exact purpose, I'll leave that as an exercise for you. If you need more help. leave a comment explaining exactly what you don't understand.

//how to get a value in your dictionary
string myValue = departments["Marketin"];

//how to update a value in your dictionary
departments["Marketing"] = 5;

//how to remove a value in your dictionary
departments.Remove("Marketin");

//how to iterate over your dictionary
foreach(var department in departments)
{
    //only print the value if it's "Marketin"
    if(department.Key == "Marketin")
    {
        Console.WriteLine(department.Value);
    }
}

I would like to sum the values for "Marketing" and "Marketin" and delete the misspelled key/value pair "Marketin".

Here is one where the user has identified the problem text and the proper text and then removes the improper(s) and adds (sums) the values to the proper key's value.

var departments = new Dictionary<string, int>()
{
    { "HR", 33 },
    { "Management", 8 },
    { "Marketing", 21 },
    { "Marketin", 4 },
    { "Sales", 44 }
};

var baseText  = "Market";
var validText = "Marketing";

var removal = departments.ToList()
                         .Where(itm => itm.Key.StartsWith(baseText) && 
                                       itm.Key.Equals(validText) == false)
                         .ToList();   

removal.ForEach(itm => departments.Remove(itm.Key));

departments[validText] += removal.Sum(itm => itm.Value);

With a final result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM