C#: Remove duplicate values from dictionary?

前端 未结 8 2535
小蘑菇
小蘑菇 2020-12-09 03:28

How can I create a dictionary with no duplicate values from a dictionary that may have duplicate values?

IDictionary myDict = new Dicti         


        
8条回答
  •  孤街浪徒
    2020-12-09 04:06

    Just a footnote to those using the Revit API, this is one method that works for me in removing duplicate elements, when you can't use say wallType as your object type and instead need to leverage raw elements. it's a beaut mate.

      //Add Pair.value to known values HashSet
                     HashSet knownValues = new HashSet();
    
                    Dictionary uniqueValues = new Dictionary();
    
                     foreach (var pair in wall_Dict)
                     {
                         if (knownValues.Add(pair.Value))
                         {
                             uniqueValues.Add(pair.Key, pair.Value);
                         }
                     }
    

提交回复
热议问题