Home > Software engineering >  How to add multiple values to a single key (Dictionaries in c#)
How to add multiple values to a single key (Dictionaries in c#)

Time:09-19

Here I am retrieving leave type and leave profile from the database. Each leave_type might have more than one profile. So I made my leave types to be keys and my profiles to be values. When I am running my code I get an exception that says : 'An item with the same key has already been added.' Please help

Dictionary<String, String> myDictionary = new Dictionary<String, String>();
            List<String[]> leave_result_list = new List<String[]>();
            StringBuilder leave_builder = new StringBuilder();`

            MySqlCommand leave_type_query = new MySqlCommand("SELECT leave_types.description, leave_profile.name FROM leave_types LEFT JOIN leave_profile on leave_types.type_id = leave_profile.leave_type_id", con);

            MySqlDataReader leave_type_reader = leave_type_query.ExecuteReader();
            while (leave_type_reader.Read())
            {
                string[] leave_type_and_profiles = new string[2];
                leave_type_and_profiles[0] = leave_type_reader.GetString("description");
                leave_type_and_profiles[1] = leave_type_reader.GetString("name");
                string leave_name = leave_type_and_profiles[0].ToString();
                string leave_profile = leave_type_and_profiles[1].ToString();
                myDictionary.Add(leave_name, leave_profile);
            }

            leave_type_reader.Close();

CodePudding user response:

I would use a Lookup instead, which works like a dictionary but with a collection of values.

void Main()
{
    var lookup = GetTheStuff().ToLookup(x => x.leave_name, x => x.leave_profile)
}

private IEnumerable<(string leave_name, string leave_profile)> GetTheStuff()
{
    MySqlCommand leave_type_query = new MySqlCommand("SELECT leave_types.description, leave_profile.name FROM leave_types LEFT JOIN leave_profile on leave_types.type_id = leave_profile.leave_type_id", con);
    MySqlDataReader leave_type_reader = leave_type_query.ExecuteReader();
    
    while (leave_type_reader.Read())
    {
        string[] leave_type_and_profiles = new string[2];
        leave_type_and_profiles[0] = leave_type_reader.GetString("description");
        leave_type_and_profiles[1] = leave_type_reader.GetString("name");
        string leave_name = leave_type_and_profiles[0].ToString();
        string leave_profile = leave_type_and_profiles[1].ToString();
        yield return (leave_name, leave_profile)
    }

    leave_type_reader.Close();
}

CodePudding user response:

Each leave_type might have more than one profile

You should better use a Dictionary<string, List<string>> then:

var myDictionary = new Dictionary<string, List<string>>();

while (leave_type_reader.Read())
{
    // ...
    if(myDictionary.TryGetValue(leave_name, out List<string> profiles))
        profiles.Add(leave_profile);
    else
        myDictionary.Add(leave_name, new List<string>{ leave_profile });
}
  •  Tags:  
  • c#
  • Related