-1

我刚刚开始,如果我不使用正确的术语,请原谅我。我试图通过循环并将一个键与集合中的所有其他键进行比较来整合 ObservableCollection>。如果它们相同,则应比较匹配的键值。我没有足够的代表来发布图片。

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

        }

4

3 回答 3

0

我对我的 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))
于 2015-08-07T19:35:50.730 回答
0

你可以这样做:

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 );
}
  • 循环遍历每个项目
  • 对于集合中的每个项目,搜索具有相同“键”的项目</li>
  • 检查“值”是否相等
于 2015-08-07T18:44:37.127 回答
0

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

    _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));
        }
于 2015-08-12T13:01:05.570 回答