-1

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

        }
Derek
  • 1
  • 5
  • I may be missing the point, but you want to loop through the Collection> and remove duplicates if the key matches and the value matches? – Speerian Aug 07 '15 at 18:43
  • basically removing redundancies if there are any, then I would blank out or set the values to a string if they aren't the same. – Derek Aug 07 '15 at 18:54
  • so you're only trying to keep duplicates in the Collection? – Speerian Aug 07 '15 at 19:15
  • Yes, I'm presenting these in a datagrid so the user can edit these values. so I need a consolidated list for the user to look at. The list is generated by selecting multiple objects in another program and pulling attributes from them. The objects share the same keys but the values could differ. – Derek Aug 07 '15 at 19:20

3 Answers3

0

You can do this like so:

public void CombineUDAs( ObservableCollection<Tuple<object, object>> UDAs )
{
    foreach ( var item in UDAs )
        foreach ( var innerItem in UDAs.Where( innerItem => innerItem != innerItem && item.Item1 == innerItem.Item1 ) )
            Console.WriteLine( "Value is same: {0}", item.Item2 == innerItem.Item2 );
}
  • Loop over each item
  • For each item search in the collection for items with the same “key”
  • Check if the “values” are equals
musium
  • 2,942
  • 3
  • 34
  • 67
0

I'm a little rusty on my c# so my syntax is probably off, and you can probably do this more cleanly, but here's a rough idea...

Note that the inner for loop starts at the outer object index so you aren't looping over duplicate objects. Might increase performance.

public void CombineUDAs( ObservableCollection<Tuple<object, object>> UDAs )
   {
       for(outer=0; outer<UDAs.Count; outer++)
          for (inner = outer; inner<UDAs.Count; inner++)
             if(outer != inner && (UDAs[inner].item1 == UDAs[outer].item1) && (UDAs[inner].item2 == UDAs[outer].item2))
                //Add to collection
   }

It may be easier to just add the elements that are duplicates to a new collection. If you're navigating through the new collection frequently it may save performance depending on the size of the collection.

If you want to make the other objects blank you'll just have to inverse the if statement as needed. Probably something like:

if(outer != inner && (UDAs[inner].item1 != UDAs[outer].item1) || (UDAs[inner].item2 != UDAs[outer].item2))
Speerian
  • 1,138
  • 1
  • 12
  • 29
0

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));
        }
Derek
  • 1
  • 5