How to compare only values in two different dictionaries which belongs to different keys in c#

StackOverflow https://stackoverflow.com/questions/18201488

Вопрос

I have Two dictionaries as Dict and Dict_BM. There content are as follows: Dict Content: Product_ID,1113

Dict 2 content : Test,1113

I just want to compare the '1113' values from both the dictionaries as 'Test' is the value for 'Product_ID' from other XML which is being captured in Dict_BM.

Code so far is as follows:

bool equal = false;
    if (Dict.Count == Dict_BM.Count) 
    {
        equal = true;
        foreach (var pair in Dict)
        {
        int value;
        if (Dict_BM.TryGetValue(pair.Key, out value))
        {
            // Require value be equal.
            if (value != pair.Value)
            {
            equal = false;
            break;
            }
        }
        else
        {
            // Require key be present.
            equal = false;
            break;
        }
        }
Это было полезно?

Решение

Test if dictionaries have the same values (but, mayhaps, the different keys)

public static Boolean DictionaryHaveEqualValues(IDictionary left, IDictionary right) {
  if (Object.ReferenceEquals(left, right))
    return true;
  else if (Object.ReferenceEquals(left, null))
    return false;
  else if (Object.ReferenceEquals(right, null))
    return false;

  if (left.Count != right.Count)
    return false;

  Dictionary<Object, int> reversed = new Dictionary<Object, int>();

  foreach (var value in left.Values)
    if (reversed.ContainsKey(value))
      reversed[value] = reversed[value] + 1;
    else
      reversed.Add(value, 1);

  foreach (var value in right.Values)
    if (!reversed.ContainsKey(value))
      return false;
    else if (reversed[value] == 1)
      reversed.Remove(value);
    else
      reversed[value] = reversed[value] - 1;

  return (reversed.Count == 0);
}

....

Dictionary<int, String> main = new Dictionary<int, String>() {
  {1, "A"},
  {2, "B"},
  {3, "A"}
};

Dictionary<int, String> test1 = new Dictionary<int, String>() {
  {7, "A"},
  {4, "B"},
  {5, "A"}
};

Dictionary<int, String> test2 = new Dictionary<int, String>() {
  {7, "A"},
  {4, "B"},
  {5, "A"},
  {9, "B"}
 };

Dictionary<int, String> test3 = new Dictionary<int, String>() {
  {7, "A"},
  {4, "C"},
  {5, "A"},
};

// Dictionarys have equal values: two "A" and one "B"
Debug.Assert(DictionaryHaveEqualValues(main, test1));

// Unequal: test2 simply has move values
Debug.Assert(!DictionaryHaveEqualValues(main, test2));

// Unequal: test3 doesn't have "B" value
Debug.Assert(!DictionaryHaveEqualValues(main, test3));

Другие советы

I guess you were trying to compare the values of two dictionaries, right?

probably this does what you want:

   var equals = (Dict.Count == Dict_BM.Count) && 
                (!Dict.Values.Except(Dict_BM.Values).Any()) && 
                (!Dict_BM.Values.Except(Dict.Values).Any());
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top