基于键的 ObservableCollection 合并

Consolidation of an ObservableCollection based on keys

我才刚刚开始,如果我使用的术语不正确,请原谅我。我正在尝试通过遍历并将一个键与集合中的所有其他键进行比较来合并 ObservableCollection>。如果它们相同,则应该比较匹配的键值。 我没有足够的代表来 post 一张照片。

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

        }

您可以这样做:

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 );
}
  • 遍历每个项目
  • 对于每个项目,在集合中搜索具有相同“键”的项目
  • 检查“值”是否相等

我的 C# 有点生疏,所以我的语法可能不对,你可以更干净地做这件事,但这里有一个粗略的想法......

请注意,内部 for 循环从外部对象索引开始,因此您不会遍历重复的对象。可能会提高性能。

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
   }

将重复的元素添加到新集合中可能更容易。如果您经常浏览新集合,它可能会节省性能,具体取决于集合的大小。

如果你想让其他对象为空,你只需要根据需要反转 if 语句。可能是这样的:

if(outer != inner && (UDAs[inner].item1 != UDAs[outer].item1) || (UDAs[inner].item2 != UDAs[outer].item2))

在同事的帮助下,它按照我想要的方式工作,这是生成的代码。枚举器是被选中的对象。我仍然需要回去收紧代码,但功能在那里。

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