Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 匹配2个字典并替换值_C#_Dictionary - Fatal编程技术网

C# 匹配2个字典并替换值

C# 匹配2个字典并替换值,c#,dictionary,C#,Dictionary,我有一本字典如下- Dictionary<string, Dictionary<int, int>> myDict1; 在更新操作myDict2之后(或者更好地说,我正在接收myDict2作为操作的返回值),它包含如下所示的值- {"Data1", {1 , 10}}; 我必须通过匹配myDict2的键来更新myDict1。 这里的键是string“Data1”和int1 注意:此处仅更改内部字典的值。 有人能提出更好的建议吗?试试这个: //sample data

我有一本字典如下-

Dictionary<string, Dictionary<int, int>> myDict1;
在更新操作myDict2之后(或者更好地说,我正在接收myDict2作为操作的返回值),它包含如下所示的值-

{"Data1", {1 , 10}};
我必须通过匹配myDict2的键来更新myDict1。 这里的键是string“Data1”和int1

注意:此处仅更改内部字典的值。

有人能提出更好的建议吗?

试试这个:

//sample data
Dictionary<string, Dictionary<int, int>> myDict1 = new Dictionary<string, Dictionary<int, int>>();            
myDict1.Add("data1", new Dictionary<int, int> { { 1, 0 }, { 2, 0 }, { 3, 0 } });
myDict1.Add("data10", new Dictionary<int, int> { { 2, 10 }, { 3, 11 }, { 4, 0 } });

Dictionary<string, Dictionary<int, int>> myDict2 = new Dictionary<string, Dictionary<int, int>>();
myDict2.Add("data1", new Dictionary<int, int> { { 1, 1 }, {3, 1 }, { 4, 1 } });
myDict2.Add("data2", new Dictionary<int, int> { { 2, 0 } });

//here we will iterate only through common keys (that both dictionaries have it)
foreach(string commonKey in myDict1.Keys.Intersect(myDict2.Keys))
    foreach(int intKey in myDict1[commonKey].Keys.Intersect(myDict2[commonKey].Keys))
        myDict2[commonKey][intKey] = myDict1[commonKey][intKey];
//示例数据
Dictionary myDict1=新字典();
添加(“data1”,新字典{{1,0},{2,0},{3,0});
myDict1.Add(“data10”,新字典{{2,10},{3,11},{4,0});
Dictionary myDict2=新字典();
myDict2.Add(“data1”,新字典{{1,1},{3,1},{4,1});
myDict2.Add(“data2”,新字典{{2,0});
//在这里,我们将只遍历公共键(这两个字典都有)
foreach(myDict1.Keys.Intersect(myDict2.Keys)中的字符串commonKey)
foreach(myDict1[commonKey].Keys.Intersect中的int-intKey(myDict2[commonKey].Keys))
myDict2[commonKey][intKey]=myDict1[commonKey][intKey];

在哪个操作之后,
myDict2
包含此值?为什么不使用枚举作为值,而不是枚举dictionary@TheGeneral因为myDict2和myDict1是同一个字典,所以键是相同的。但在一些操作之后,myDict2的内部字典值被更改。匹配这两个字典的键,我需要更新内部字典“myDict1”的值。@nfgallimore enum不符合我的目的,因为我收到的响应是dictionary@Md.ParwezAkhtar每本字典里有多少条记录?每次都是一张唱片吗?
//sample data
Dictionary<string, Dictionary<int, int>> myDict1 = new Dictionary<string, Dictionary<int, int>>();            
myDict1.Add("data1", new Dictionary<int, int> { { 1, 0 }, { 2, 0 }, { 3, 0 } });
myDict1.Add("data10", new Dictionary<int, int> { { 2, 10 }, { 3, 11 }, { 4, 0 } });

Dictionary<string, Dictionary<int, int>> myDict2 = new Dictionary<string, Dictionary<int, int>>();
myDict2.Add("data1", new Dictionary<int, int> { { 1, 1 }, {3, 1 }, { 4, 1 } });
myDict2.Add("data2", new Dictionary<int, int> { { 2, 0 } });

//here we will iterate only through common keys (that both dictionaries have it)
foreach(string commonKey in myDict1.Keys.Intersect(myDict2.Keys))
    foreach(int intKey in myDict1[commonKey].Keys.Intersect(myDict2[commonKey].Keys))
        myDict2[commonKey][intKey] = myDict1[commonKey][intKey];