0

Is it possible to make a method that takes a Dictionary with a key of generic type and a value of IEnumerable of generics? Such as

private static bool AreDictionariesOfObjectAndListEqual<TKey, IEnumerable<T>>(Dictionary<TKey, IEnumerable<T>> dict1, Dictionary<TKey, IEnumerable<T>> dict2)
{
    var dict1String = string.Join(",",
                dict1.OrderBy(kv => kv.Key).Select(kv => kv.Key + ":" + string.Join("|", kv.Value.OrderBy(v => v))));
    var dict2String = string.Join(",",
                dict2.OrderBy(kv => kv.Key).Select(kv => kv.Key + ":" + string.Join("|", kv.Value.OrderBy(v => v))));

    return dict1String.Equals(dict2String);
}

The equality code comes directly from the answer here: https://stackoverflow.com/a/45606892/8657968

1 Answers1

0

You could use something like this instead:

private static bool AreDictionariesOfObjectAndListEqual<TKey, TValue>(Dictionary<TKey, IEnumerable<TValue>> dict1, Dictionary<TKey, IEnumerable<TValue>> dict2)
{
    var dict1String = string.Join(",",
                dict1.OrderBy(kv => kv.Key).Select(kv => kv.Key + ":" + string.Join("|", kv.Value.OrderBy(v => v))));
    var dict2String = string.Join(",",
                dict2.OrderBy(kv => kv.Key).Select(kv => kv.Key + ":" + string.Join("|", kv.Value.OrderBy(v => v))));

    return dict1String.Equals(dict2String);
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120