I want to compare the contents of two Dictionary
instances regardless of the order of the items they contain. SequenceEquals
This will check if all Values
from source
exists in target
, ignoring the Keys
var result = source.All(x => target.Any(y => x.Value == y.Value));
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;
}
If you use a SortedDictionary you won't need to apply the sorting yourself, which can be slightly easier to use:
void Main()
{
var d1 = new Dictionary<string, string>
{
["a"] = "Hi there!",
["b"] = "asd",
["c"] = "def"
};
var d2 = new Dictionary<string, string>
{
["b"] = "asd",
["a"] = "Hi there!",
["c"] = "def"
};
var sortedDictionary1 = new SortedDictionary<string, string>(d1);
var sortedDictionary2 = new SortedDictionary<string, string>(d2);
if (sortedDictionary1.SequenceEqual(sortedDictionary2))
{
Console.WriteLine("Match!");
}
else
{
Console.WriteLine("Not match!");
}
}
I don't know if there is an existing method but you could use the following (null checking of args omitted for brevity)
public static bool DictionaryEquals<TKey,TValue>(
this Dictionary<TKey,TValue> left,
Dictionary<TKey,TValue> right ) {
var comp = EqualityComparer<TValue>.Default;
if ( left.Count != right.Count ) {
return false;
}
foreach ( var pair in left ) {
TValue value;
if ( !right.TryGetValue(pair.Key, out value)
|| !comp.Equals(pair.Value, value) ) {
return false;
}
}
return true;
}
It would be best to add an overload to allow customization of the EqualityComparer<TValue>
.