能否在合并字典的时也对字典值进行处理

shijies 2022-05-19 15:16:37

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO;

using System.Xml.Serialization;

using System.Xml;

using System.Xml.Schema;

 

 

 

 

 

 

namespace WindowsFormsApp2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        SerializableDictionary<string, List<string>> serializableDictionary;

        SerializableDictionary<string, List<string>> serializableDictionary1;

 

 

 

        private void Form1_Load(object sender, EventArgs e)

        {

            using (FileStream fileStream = new FileStream("Dict.xml", FileMode.Open))

            {

                XmlSerializer xmlFormatter = new XmlSerializer(typeof(SerializableDictionary<string, List<string>>));

                serializableDictionary = (SerializableDictionary<string, List<string>>)xmlFormatter.Deserialize(fileStream);

 

            }

 

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件|*.*";

            ofd.ValidateNames = true;

            ofd.CheckPathExists = true;

            ofd.CheckFileExists = true;

            if (ofd.ShowDialog() == DialogResult.OK)

            {

                string strFileName = ofd.FileName;

                using (FileStream fileStream = new FileStream(strFileName, FileMode.Open))

                {

                    XmlSerializer xmlFormatter = new XmlSerializer(typeof(SerializableDictionary<string, List<string>>));

                    serializableDictionary1 = (SerializableDictionary<string, List<string>>)xmlFormatter.Deserialize(fileStream);

                    foreach (KeyValuePair<string, List<string>> item in serializableDictionary1)

                    {

                        richTextBox2.Text += item.Key;

                        richTextBox2.Text += Environment.NewLine;

                        foreach (string s in item.Value)

                        {

                            richTextBox2.Text += s + "  ";

                        }

                        richTextBox2.Text += Environment.NewLine;

                    }

                }

            }

 

        }

 

 

        private void button2_Click(object sender, EventArgs e)

        {

            foreach (var item in serializableDictionary1)

                             {

                if (!serializableDictionary.ContainsKey(item.Key))

                { serializableDictionary.Add(item.Key, item.Value); }

                else

                { List<string> serializableDictionary[item.Key] = serializableDictionary[item.Key].Union(item.Value).ToList<string>(); }

                             }

 

 

           

            richTextBox3.Text = "";

            foreach (KeyValuePair<string, List<string>> item in serializableDictionary)

            {

                richTextBox3.Text += item.Key;

                richTextBox3.Text += Environment.NewLine;

                foreach (string s in item.Value)

                {

                    richTextBox3.Text += s + "  ";

                }

                richTextBox3.Text += Environment.NewLine;

            }

        }

 

      

    }

}

以上代码先声明了两个字典:

        SerializableDictionary<string, List<string>> serializableDictionary;

        SerializableDictionary<string, List<string>> serializableDictionary1;

如果没有以下的代码:

  else

                { List<string> serializableDictionary[item.Key] = serializableDictionary[item.Key].Union(item.Value).ToList<string>(); }

可以编译通过,可对上述两个字典进行合并,但是若两个字典有相同的key,合并后字典key的value还是原来的,我想将两个字典有相同的key的value(List<string> 类型的集合合并),增加了的代码:

else

                { List<string> serializableDictionary[item.Key] = serializableDictionary[item.Key].Union(item.Value).ToList<string>(); }

在编译时会导致异常出现,提示如下:

 

严重性

代码

说明

项目

文件

禁止显示状态

错误

CS0650

错误的数组声明符: 要声明托管数组,秩说明符应位于变量标识符之前。要声明固定大小缓冲区字段,应在字段类型之前使用 fixed 关键字。

WindowsFormsApp2

F:\Dictionary\合并字典\WindowsFormsApp2\Form1.cs

96

活动的

请教!

...全文
91 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghui0380 2022-05-19
  • 打赏
  • 举报
回复

另一个问题也是你问的把,我们没有回答是因为你没有说明合并规则---也就是那位5年新人兄喜欢到处凑数 :-(
你现在既然给了合并规则---key一样使用原有的,那么我们说那就按你的想法写就是,没有linq这类简单方法。
写循环判定key,有key不更新,没key就新增

当然我们可以给个提示:ConcurrentDictionary<T,T2> 做会方便点,这个是线程安全字典,在多线程的条件下操作是竞争关系,你没办法确定到底是Add还是update(这个我想你可以理解),所以他提供的方法是AddOrUpdate,写法上可以少写点代码,你可以试试下面的代码

using System.Collections.Concurrent;

ConcurrentDictionary<int, int> dic = new ConcurrentDictionary<int, int>();
ConcurrentDictionary<int, int> dic2 = new ConcurrentDictionary<int, int>();
//我们构造一些数据
dic.TryAdd(1, 1);
dic.TryAdd(2, 2);

dic2.TryAdd(1, 10);
dic2.TryAdd(3, 30);

//我们来合并他,当然按你的规则他最后是[{1,1},{2,2},{3,30}]

foreach (var keyValuePair in dic2)
{
//AddORUpdate 添加或更新,更新方法是个func<int,int,int> 也就是 旧值,新值都在,你自己在委托里定义更新规则
//这里按你的规则,更新规则为直接保留旧值
    dic.AddOrUpdate(keyValuePair.Key,keyValuePair.Value,(oldvalue,newvalue)=>oldvalue);
}



111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧