比较2字典&LT;字符串,字符串&GT;实例 [英] Comparing 2 Dictionary<string, string> Instances
本文介绍了比较2字典&LT;字符串,字符串&GT;实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要两个词典1所述的内容相比较,字符串,字符串&GT;
实例,不论它们所包含的项目的顺序。 SequenceEquals的
也比较顺序,所以我第一次命令由密钥字典,然后调用 SequenceEquals的
。
有没有办法,我可以改用 SequenceEquals的
的只会比较内容的方法?
如果没有的话,这是做到这一点的理想方式?
词典&LT;字符串,字符串&GT;来源=新词典&LT;字符串,字符串&GT;();
字典&LT;字符串,字符串&GT;目标=新词典&LT;字符串,字符串&GT;();来源[富] =酒吧;
来源[巴兹] =捷思;
来源[嗒嗒] = NULL;目标[巴兹] =捷思;
目标[嗒嗒] = NULL;
目标[富] =酒吧;// SequenceEquals的会是假的
VAR sequenceEqual = source.SequenceEqual(目标);
// contentsEqual为真
VAR contentsEqual = source.OrderBy(X =&GT; x.Key).SequenceEqual(target.OrderBy(X =&GT; x.Key));
解决方案
VAR contentsEqual = source.DictionaryEqual(目标);// ...公共静态布尔DictionaryEqual&LT; TKEY的,TValue&GT;(
这IDictionary的&LT; TKEY的,TValue&GT;第一,IDictionary的&LT; TKEY的,TValue&GT;第二)
{
返回first.DictionaryEqual(第二,零);
}公共静态布尔DictionaryEqual&LT; TKEY的,TValue&GT;(
这IDictionary的&LT; TKEY的,TValue&GT;第一,IDictionary的&LT; TKEY的,TValue&GT;第二,
的IEqualityComparer&LT; TValue&GT; valueComparer)
{
如果(第一==二)返回true;
如果((第一== NULL)||(第二== NULL))返回false;
如果(first.Count = second.Count!)返回false; valueComparer = valueComparer? EqualityComparer&LT; TValue&GT; .DEFAULT; 的foreach(第一VAR KVP)
{
TValue secondValue;
如果返回false(second.TryGetValue(kvp.Key,出secondValue)!);
如果(!valueComparer.Equals(kvp.Value,secondValue))返回false;
}
返回true;
}
I want to compare the contents of two Dictionary<string, string>
instances regardless of the order of the items they contain. SequenceEquals
also compares the order, so I first order the dictionaries by key and then call SequenceEquals
.
Is there a method that I can use instead of SequenceEquals
that will only compare the contents?
If there isn't, is this the ideal way to do this?
Dictionary<string, string> source = new Dictionary<string, string>();
Dictionary<string, string> target = new Dictionary<string, string>();
source["foo"] = "bar";
source["baz"] = "zed";
source["blah"] = null;
target["baz"] = "zed";
target["blah"] = null;
target["foo"] = "bar";
// sequenceEquals will be false
var sequenceEqual = source.SequenceEqual(target);
// contentsEqual will be true
var contentsEqual = source.OrderBy(x => x.Key).SequenceEqual(target.OrderBy(x => x.Key));
解决方案
var contentsEqual = source.DictionaryEqual(target);
// ...
public static bool DictionaryEqual<TKey, TValue>(
this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second)
{
return first.DictionaryEqual(second, null);
}
public static bool DictionaryEqual<TKey, TValue>(
this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second,
IEqualityComparer<TValue> valueComparer)
{
if (first == second) return true;
if ((first == null) || (second == null)) return false;
if (first.Count != second.Count) return false;
valueComparer = valueComparer ?? EqualityComparer<TValue>.Default;
foreach (var kvp in first)
{
TValue secondValue;
if (!second.TryGetValue(kvp.Key, out secondValue)) return false;
if (!valueComparer.Equals(kvp.Value, secondValue)) return false;
}
return true;
}
这篇关于比较2字典&LT;字符串,字符串&GT;实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文