C#:从字典中删除重复值?

C#:从字典中删除重复值?,c#,linq,.net-3.5,dictionary,C#,Linq,.net 3.5,Dictionary,如何从可能具有重复值的词典中创建没有重复值的词典 IDictionary<string, string> myDict = new Dictionary<string, string>(); myDict.Add("1", "blue"); myDict.Add("2", "blue"); myDict.Add("3", "red"); myDict.Add("4", "green"); uniqueValueDict = myDict.??? IDictiona

如何从可能具有重复值的词典中创建没有重复值的词典

IDictionary<string, string> myDict = new Dictionary<string, string>();

myDict.Add("1", "blue");
myDict.Add("2", "blue");
myDict.Add("3", "red");
myDict.Add("4", "green");


uniqueValueDict = myDict.???
IDictionary myDict=new Dictionary();
myDict.添加(“1”、“蓝色”);
myDict.添加(“2”、“蓝色”);
myDict.添加(“3”,“红色”);
myDict.添加(“4”、“绿色”);
uniqueValueDict=myDict。???
编辑:

-我不管哪把钥匙留着。
-是否有使用Distinct()操作的内容?

要如何处理重复项?如果您不介意丢失哪个键,只需构建另一个字典,如下所示:

IDictionary<string, string> myDict = new Dictionary<string, string>();

myDict.Add("1", "blue");
myDict.Add("2", "blue");
myDict.Add("3", "red");
myDict.Add("4", "green");

HashSet<string> knownValues = new HashSet<string>();
Dictionary<string, string> uniqueValues = new Dictionary<string, string>();

foreach (var pair in myDict)
{
    if (knownValues.Add(pair.Value))
    {
        uniqueValues.Add(pair.Key, pair.Value);
    }
}

蛮力解决方案如下所示

var result = dictionary
    .GroupBy(kvp => kvp.Value)
    .ToDictionary(grp => grp.First().Value, grp.Key)
假设您并不真正关心用于表示一组重复项的键,并且可以重新生成字典。

dictionary test=new dictionary();
Dictionary<string, string> test = new Dictionary<string,string>();
test.Add("1", "blue");
test.Add("2", "blue");
test.Add("3", "green");
test.Add("4", "red");
Dictionary<string, string> test2 = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> entry in test)
{
    if (!test2.ContainsValue(entry.Value))
        test2.Add(entry.Key, entry.Value);
}
测试。添加(“1”、“蓝色”); 测试。添加(“2”,“蓝色”); 测试。添加(“3”,“绿色”); 测试。添加(“4”,“红色”); Dictionary test2=新字典(); foreach(测试中的KeyValuePair条目) { 如果(!test2.ContainsValue(entry.Value)) test2.Add(entry.Key,entry.Value); }
Jon抢先介绍了.NET 3.5解决方案,但如果您需要.NET 2.0解决方案,这应该是可行的:

        List<string> vals = new List<string>();
        Dictionary<string, string> newDict = new Dictionary<string, string>();
        foreach (KeyValuePair<string, string> item in myDict)
        {
            if (!vals.Contains(item.Value))
            {
                newDict.Add(item.Key, item.Value);
                vals.Add(item.Value);
            }
        }
List vals=new List();
字典newDict=新字典();
foreach(myDict中的KeyValuePair项)
{
如果(!vals.Contains(item.Value))
{
newDict.Add(item.Key,item.Value);
增值(项目价值);
}
}

除了Jon Skeet的答案外,如果您的值是intern对象,则可以使用:

var uniqueValues = myDict.GroupBy(pair => pair.Value.Property)
                     .Select(group => group.First())
                     .ToDictionary(pair => pair.Key, pair => pair.Value);
这样,您将只删除对象的一个属性上的副本

我就是这样做的:

                dictionary.add(control, "string1");
                dictionary.add(control, "string1");
                dictionary.add(control, "string2");
              int x = 0;
        for (int i = 0; i < dictionary.Count; i++)
        {         
            if (dictionary.ElementAt(i).Value == valu)
            {
                x++;
            }
            if (x > 1)
            {
                dictionary.Remove(control);
            }
        }
dictionary.add(控件,“string1”);
添加(控件,“string1”);
添加(控件,“string2”);
int x=0;
for(int i=0;i1)
{
删除(控制);
}
}

对于那些使用Revit API的人来说,这只是一个脚注,对于我来说,当您不能使用say wallType作为对象类型,而需要利用原始图元时,这是一种删除重复图元的方法。这是一个美丽的伴侣

  //Add Pair.value to known values HashSet
                 HashSet<string> knownValues = new HashSet<string>();

                Dictionary<Wall, string> uniqueValues = new Dictionary<Wall, string>();

                 foreach (var pair in wall_Dict)
                 {
                     if (knownValues.Add(pair.Value))
                     {
                         uniqueValues.Add(pair.Key, pair.Value);
                     }
                 }
//将Pair.value添加到已知值HashSet
HashSet knownValues=新HashSet();
Dictionary uniqueValues=新字典();
foreach(墙壁上的var对)
{
if(knownvalue.Add(pair.Value))
{
uniqueValues.Add(pair.Key,pair.Value);
}
}

您想保留哪把钥匙<代码>“1”,
“2”
,或者没有?我认为您需要提供有关独特生成函数行为的更多信息。对于蓝色,它应该保留哪一个键,1还是2?我试图想象像linq一样的解决方案,但我的指尖上没有VS+1实现这种方法;-)没有编译,因为我在第一次()调用后错过了.Value,但修复了它。这对我不起作用。它创建了一个以“value”作为键和值的字典。这将生成一个以值作为键和值的字典!:-(他不知道在100K之后,它会循环回0,RuuhahahaThank。linq解决方案正是我所寻找的。好奇的是,你能不能以某种方式使用Distinct扩展方法?@User:我认为Distinct在这种情况下不会有帮助……但与MoreLINQ不同。linq方法在所有情况下都不起作用。一些字典集不允许我们希望你调用.GroupBy()方法。@Mitchell:你能提供更多细节吗?你说的“字典集”是什么意思?
                dictionary.add(control, "string1");
                dictionary.add(control, "string1");
                dictionary.add(control, "string2");
              int x = 0;
        for (int i = 0; i < dictionary.Count; i++)
        {         
            if (dictionary.ElementAt(i).Value == valu)
            {
                x++;
            }
            if (x > 1)
            {
                dictionary.Remove(control);
            }
        }
  //Add Pair.value to known values HashSet
                 HashSet<string> knownValues = new HashSet<string>();

                Dictionary<Wall, string> uniqueValues = new Dictionary<Wall, string>();

                 foreach (var pair in wall_Dict)
                 {
                     if (knownValues.Add(pair.Value))
                     {
                         uniqueValues.Add(pair.Key, pair.Value);
                     }
                 }