C# 如何在C中添加、更新、保存和删除密钥列表

C# 如何在C中添加、更新、保存和删除密钥列表,c#,keyvaluepair,C#,Keyvaluepair,具体问题 我想更新KeyValue对,如果我删除了某些内容,它应该被删除,其余的项目也会被保存,但当我删除一个键时,每个值都会被删除。我只想删除一个特定的值,保留其余的值并将其保存回去 编辑:显然,如果我删除一个键,每个值都会被删除,我想从键中删除一个特定值,并保留键中的剩余值 注意:如果我的问题是错误的,你可以建议编辑 一点背景: 第一个组合框为A类,第二个组合框为B类。 当我选择category A时,我会生成一些序列,同时添加到comboBox2以进行显示,并添加到keyvalue对 与B

具体问题 我想更新KeyValue对,如果我删除了某些内容,它应该被删除,其余的项目也会被保存,但当我删除一个键时,每个值都会被删除。我只想删除一个特定的值,保留其余的值并将其保存回去

编辑:显然,如果我删除一个键,每个值都会被删除,我想从键中删除一个特定值,并保留键中的剩余值

注意:如果我的问题是错误的,你可以建议编辑

一点背景:

第一个组合框为A类,第二个组合框为B类。 当我选择category A时,我会生成一些序列,同时添加到comboBox2以进行显示,并添加到keyvalue对

与B类相同

当我选择类别A时,我只想在类别A中看到生成的序列,当我选择类别B时,我只想在类别B中看到生成的序列

这就是为什么我使用KeyValuePair,我找不到其他更好的解决方案,我用json保存文件,当我加载应用程序时,我重新加载文件并用数据重新填充组合框

现在我想使用一个特定的序列号,当使用该序列号时,我想将其从组合框中删除,同时从keyvalue对中删除并保存现有数据

这是一张更好地理解它的图片。

全局变量:

重新填充组合框

如何删除密钥并保存剩余数据

//这可能是错误的做法。 //我想删除在comboBox2中选择的值

   private void DeleteButton_Click(object sender, EventArgs e)
    {
   // Deserialise it from Disk back to a Dictionary
        string jsonToRead = File.ReadAllText("c:\\Dictionary.txt");

        List<KeyValuePair<int, string>> myDictionaryReconstructed =
            JsonConvert.DeserializeObject<List<KeyValuePair<int, string>>>(jsonToRead);

        foreach (var item in myDictionaryReconstructed)
        {
            if (item.Key == 0) //If Key value is 0, if it is CategoryA
            {
                if (item.Value == this.comboBox1.SelectedValue.ToString())
                {
                    vals.Remove(new KeyValuePair<int, string>(0, comboBox1.SelectedValue.ToString()));
                }
            }

        }

        string json = JsonConvert.SerializeObject(myDictionaryReconstructed);
        using (StreamWriter sw = File.CreateText("C:\\Dictionary.txt"))
        {
            sw.Write(json);
        }


    }

我认为这与foreach循环有关,对我来说这似乎是不必要的

你能试着把每一个街区都搬走吗

DeleteButton_Click 
事件并将其替换为

vals.Remove(new KeyValuePair<int, string>(0, comboBox1.SelectedItem.ToString()));

VAL.Addnew KeyValuePair0,项目;您总是将密钥对设置为0,什么的?对于类别A,我使用0,对于类别B,我使用1。vals.Removenew KeyValuePair0,comboBox2.SelectedItem.ToString;使用此选项将删除整个键,而不仅仅是值。请尝试使用IndexOf获取元素所在的位置并删除一组VAL,这样您就可以强制只删除一个键,以确保Remove实际上可以正常工作。我认为这与foreach循环有关,对我来说这似乎是不必要的@帕特里克,你真的调试过这个行为吗?请尝试删除DeleteButton\u Click事件中的每个块,并将其替换为VAL。Removenew KeyValuePair0,comboBox1.SelectedItem.ToString@uteist我删除了for循环,它工作了,只有comboBox没有更新il检查。。。
string jsonToRead = File.ReadAllText("c:\\Dictionary.txt");

        List<KeyValuePair<int, string>> myDictionaryReconstructed = 
            JsonConvert.DeserializeObject<List<KeyValuePair<int, string>>>(jsonToRead);

        if (comboBox1.SelectedIndex == 0)
        {
            comboBox2.Items.Clear();
            foreach (var item in myDictionaryReconstructed)
            {
                if (item.Key == 0) //If Key value is 0, if it is CategoryA
                {
                    comboBox2.Items.Add(item.Value);
                    comboBox2.Refresh();
                }
            }
   private void DeleteButton_Click(object sender, EventArgs e)
    {
   // Deserialise it from Disk back to a Dictionary
        string jsonToRead = File.ReadAllText("c:\\Dictionary.txt");

        List<KeyValuePair<int, string>> myDictionaryReconstructed =
            JsonConvert.DeserializeObject<List<KeyValuePair<int, string>>>(jsonToRead);

        foreach (var item in myDictionaryReconstructed)
        {
            if (item.Key == 0) //If Key value is 0, if it is CategoryA
            {
                if (item.Value == this.comboBox1.SelectedValue.ToString())
                {
                    vals.Remove(new KeyValuePair<int, string>(0, comboBox1.SelectedValue.ToString()));
                }
            }

        }

        string json = JsonConvert.SerializeObject(myDictionaryReconstructed);
        using (StreamWriter sw = File.CreateText("C:\\Dictionary.txt"))
        {
            sw.Write(json);
        }


    }
DeleteButton_Click 
vals.Remove(new KeyValuePair<int, string>(0, comboBox1.SelectedItem.ToString()));