Comparing 2 Dictionary Instances

后端 未结 4 1815
忘掉有多难
忘掉有多难 2020-12-03 09:33

I want to compare the contents of two Dictionary instances regardless of the order of the items they contain. SequenceEquals

相关标签:
4条回答
  • 2020-12-03 10:11

    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));
    
    0 讨论(0)
  • 2020-12-03 10:12
    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;
    }
    
    0 讨论(0)
  • 2020-12-03 10:25

    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!");
        }
    }
    
    0 讨论(0)
  • 2020-12-03 10:26

    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>.

    0 讨论(0)
提交回复
热议问题