C#将值从一个字典替换为另一个字典

时间:2017-02-13 23:43:43

标签: c# object dictionary

我有2个词典,键相同但值不同。我需要在dictionary1中找到所有等于1的值,并用dictionary2中的值替换它们。我可以编辑第一个字典或更好的想法是将新值保存到另一个字典中,无论如何都很好。我的想法是使用foreach循环,然后在dictionary1 == 1中找到任何值,然后进入dictionary2并提取该键的值并将其保存到dictionary1或该键的另一个字典中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice_With_Dictionaries
{
    class Program
    {
        static void Main(string[] args)
        {


                object[] keys= new object[5];
                keys[0] = "1111";
                keys[1] = "2222";
                kesy[2] = "3333";
                kesy[3] = "4444";
                kesy[4] = "5555";


                object[] speed = new object[5];
                speed[0] = 1;
                speed[1] = 2;
                speed[2] = 1;
                speed[3] = 1;
                speed[4] = 5;

                object[] speed2 = new object[5];
                speed2[0] = 1;
                speed2[1] = 2;
                speed2[2] = 3;
                speed2[3] = 4;
                speed2[4] = 5;





   Dictionary<object, object> dictionary1 = new Dictionary<object,object>();
   Dictionary<object, object> dictionar2 = new Dictionary<object, object>();

                for (int iRow = 0; iRow < interfaceKeys.Length; iRow++)
                {

                  dictionary1.Add(keys[iRow], (double)speed[iRow]);
                  dictionary2.Add(keys[iRow], (double)speed2[iRow]);


                }


                // for each loop with if statements




        }
    }
}

3 个答案:

答案 0 :(得分:0)

首先,如果您将string作为键,int作为值,则应使用Dictionary<string, int>代替Dictionary<object, object>

Dictionary<string, int> dictionary1 = new Dictionary<string, int>();
Dictionary<string, int> dictionary2 = new Dictionary<string, int>();
// fill your dictionaries

现在,如果你总是在两个词典之间有匹配的键:

var keys = dictionary1.Where(kvp => kvp.Value == 1).Select(kvp => kvp.Key);
foreach (var key in keys)
    dictionary1[key] = dictionary2[key];

否则

var keys = dictionary1.Where(kvp => kvp.Value == 1).Select(kvp => kvp.Key);
foreach (var key in keys)
{
    int value;
    if (dictionary2.TryGetValue(key, out value))
        dictionary1[key] = value;
}

答案 1 :(得分:0)

您可以使用LINQ和ToDictionary方法(我做了一些更改以简化您的代码,我希望这是可以接受的):

static void Main(string[] args)
{
    var keys = new string[5] { "1111", "2222", "3333", "4444", "5555" };

    var speed = new double[5] { 1, 2, 1, 1, 5 };

    var speed2 = new double[5] { 1, 2, 3, 4, 5 };

    var dictionary1 = new Dictionary<string, double>();
    var dictionary2 = new Dictionary<string, double>();

    for (int iRow = 0; iRow < keys.Length; iRow++)
    {
        dictionary1.Add(keys[iRow], speed[iRow]);
        dictionary2.Add(keys[iRow], speed2[iRow]);
    }

    // the solution
    var result = dictionary1.ToDictionary(x => x.Key, kv => kv.Value == 1 ? dictionary2[kv.Key] : kv.Value);
}

答案 2 :(得分:0)

你可以做这样的事情

var dictionary1 = new Dictionary<string, string>
{
    {"a", "1"},
    {"b", "2"},
    {"c", "3"},
    {"d", "3"}
};



var dictionary2 = new Dictionary<string, string>
{
    {"a", "1"},
    {"b", "22"},
    {"c", "33"}
};

var result = dictionary1
    .Where(d => dictionary2.ContainsKey(d.Key) && dictionary2[d.Key] == d.Value);

结果将包含dictinary1和dictinary2中具有相同键的所有值,其值为dictinary1。