是否可以使方法接受Dictionary <tkey,ienumerable <t =“” >>?

时间:2019-04-03 17:50:01

标签: c#

是否有可能使一个方法接受一个具有泛型类型键和IEnumerable泛型值的Dictionary?如

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);
}

相等代码直接来自此处的答案:https://stackoverflow.com/a/45606892/8657968

1 个答案:

答案 0 :(得分:0)

您可以改用以下方式:

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);
}