【问题标题】:Compare Two Ordered Dictionaries比较两个有序字典
【发布时间】:2016-10-22 17:45:11
【问题描述】:

背景

当我更新网格中的值时,我创建了两个有序字典(旧值和新值)。然后,我想比较哪些值不同,并对我的数据源(恰好是一个列表)进行更改。

代码

这是我创建的用于比较两个类型为 Dictionary<string,T> 的字典的方法

private Dictionary<string, string> FindChangedValues(OrderedDictionary newValues, OrderedDictionary oldValues)
{
    Dictionary<string, string> _dictKPVtoUpdate = new Dictionary<string, string>();

    foreach (KeyValuePair<string, string> newItem in newValues)
    {
        foreach (KeyValuePair<string, string> oldItem in oldValues)
        {
            if (newItem.Key == oldItem.Key)
            {
                if (!newItem.Value.ToString().Equals(oldItem.Value.ToString(), StringComparison.InvariantCultureIgnoreCase))
                {
                    _dictKPVtoUpdate.Add(oldItem.Key, newItem.Value);

                }
            }
        }
    }

    return _dictKPVtoUpdate;
}

问题

我似乎无法将字典的值转换为字符串,出现以下异常。

指定的转换无效。

在这条线上

foreach (KeyValuePair<string, string> newItem in newValues)

问题

有没有更好的方法来获取两个有序字典之间的变化?

如何将每个值转换为字符串以进行比较,或者有没有办法直接比较它们而不进行转换?

编辑:

回答

正如所指出的,我使用的是KeyValuePair 而不是DictionaryEntry

将代码更改为以下,问题已解决。

更改代码

private Dictionary<string, string> FindChangedValues(OrderedDictionary newValues, OrderedDictionary oldValues)
{
    Dictionary<string, string> _dictKPVtoUpdate = new Dictionary<string, string>();

    foreach (DictionaryEntry newItem in newValues)
    {
        foreach (DictionaryEntry oldItem in oldValues)
        {

            if (newItem.Key.ToString() == oldItem.Key.ToString())
            {
                if (!newItem.Value.ToString().Equals(oldItem.Value.ToString(), StringComparison.InvariantCultureIgnoreCase))
                {
                    _dictKPVtoUpdate.Add(oldItem.Key.ToString(), newItem.Value.ToString());

                }
            }
        }
    }

    return _dictKPVtoUpdate;
}

【问题讨论】:

    标签: c# dictionary ordereddictionary


    【解决方案1】:

    遍历字典是低效的。 我会利用字典哈希并像这样实现它:

                Dictionary<string, string> _dictKPVtoUpdate = new Dictionary<string, string>();
            OrderedDictionary newValues =new OrderedDictionary();
            OrderedDictionary oldValues = new OrderedDictionary();
    
            foreach (DictionaryEntry tmpEntry in newValues)
            {
                if (oldValues.Contains(tmpEntry.Key))
                {
                    _dictKPVtoUpdate.Add(tmpEntry.Key.ToString(),tmpEntry.Value.ToString());
                }
            }
    

    【讨论】:

      【解决方案2】:

      DictionaryEntry 用于OrderedDictionary 而不是KeyValuePair。转换为 DictionaryEntry 并使用其 Key/Value 属性。

      每个元素都是存储在DictionaryEntry 对象中的键/值对。 键不能为空,但值可以。

      OrderedDictionary/Remarks

      【讨论】:

      • 还值得注意的是DictionaryEntry 的键和值是object 类型,因此需要转换为字符串以放入用于输出的Dictionary&lt;string,string&gt;(或不同的可以使用返回类型)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2014-03-03
      相关资源
      最近更新 更多