Search code examples
c#observablecollectionconsolidation

Consolidation of an ObservableCollection based on keys


I'm just starting out so forgive me if I don't use the correct terminology. I'm trying to consolidate an ObservableCollection> by looping through and comparing one key to all the other keys in the collection. If they are the same it should then compare the matching keys values. I don't have enough rep to post a pic.

        private void CombineUDAs(ObservableCollection<Tuple<object, object>> UDAs)
        {
            foreach (var item in UDAs)
            {
                
            }

        }


Solution

  • Got it working the way I wanted with the help of a coworker here's the resulting code. Enumerator is the selected objects. I still need to go back to tighten the code up but the functionality is there.

        _udaTuple = new ObservableCollection<Tuple<object, object>>();        
        var tempDictionary = new Dictionary<object, object>();
        foreach (var item in Enumerator)
            {
                var modelObject = item as TSM.ModelObject;
    
                if (modelObject != null)
                {
                    var tempHash = new Hashtable();
                    modelObject.GetAllUserProperties(ref tempHash);
                    foreach (DictionaryEntry dictionaryEntry in tempHash)
                    {
                        if (tempDictionary.ContainsKey(dictionaryEntry.Key))
                        {
                            if (tempDictionary[dictionaryEntry.Key] is string && dictionaryEntry.Value is string)
                            {
                                if ((string)tempDictionary[dictionaryEntry.Key]!=(string)dictionaryEntry.Value)
                                {
                                    tempDictionary[dictionaryEntry.Key] = "Values Do Not Match";
                                }
                            }
                            else if (tempDictionary[dictionaryEntry.Key] is double && dictionaryEntry.Value is double)
                            {
                                if ((double)tempDictionary[dictionaryEntry.Key] != (double)dictionaryEntry.Value)
                                {
                                    tempDictionary[dictionaryEntry.Key] = "Values Do Not Match";
                                }
                            }
                            else if (tempDictionary[dictionaryEntry.Key] is int && dictionaryEntry.Value is int)
                            {
                                if ((int)tempDictionary[dictionaryEntry.Key] != (int)dictionaryEntry.Value)
                                {
                                    tempDictionary[dictionaryEntry.Key] = "Values Do Not Match";
                                }
                            }
                        }
                        else
                        {
                            tempDictionary.Add(dictionaryEntry.Key, dictionaryEntry.Value);
                        }
                    }
                }
            }
            foreach (var item in tempDictionary)
            {
                _udaTuple.Add(new Tuple<object, object>(item.Key, item.Value));
            }