C# 根据值删除字典中的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1636885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:35:19  来源:igfitidea点击:

Remove Item in Dictionary based on Value

c#.netlinqdictionary

提问by Jon

I have a Dictionary<string, string>.

我有一个Dictionary<string, string>.

I need to look within that dictionary to see if a value exists based on input from somewhere else and if it exists remove it.

我需要在该字典中查看是否存在基于其他地方输入的值,如果存在则将其删除。

ContainsValue just says true/false and not the index or key of that item.

containsValue 只是说真/假,而不是该项目的索引或键。

Help!

帮助!

Thanks

谢谢

EDIT:Just found this - what do you think?

编辑:刚刚发现这个 - 你怎么看?

var key = (from k in dic where string.Compare(k.Value, "two", true) ==
0 select k.Key).FirstOrDefault();

EDIT 2:I also just knocked this up which might work

编辑 2:我也刚刚敲了这可能有用

foreach (KeyValuePair<string, string> kvp in myDic)
{
    if (myList.Any(x => x.Id == kvp.Value))
        myDic.Remove(kvp.Key);
}

采纳答案by Paul Turner

Are you trying to remove a single value or all matching values?

您是要删除单个值还是所有匹配的值?

If you are trying to remove a single value, how do you define the value you wish to remove?

如果您尝试删除单个值,您如何定义要删除的值?

The reason you don't get a key back when querying on values is because the dictionary could contain multiple keys paired with the specified value.

查询值时没有取回键的原因是字典可能包含与指定值配对的多个键。

If you wish to remove all matching instances of the same value, you can do this:

如果您希望删除具有相同值的所有匹配实例,您可以这样做:

foreach(var item in dic.Where(kvp => kvp.Value == value).ToList())
{
    dic.Remove(item.Key);
}

And if you wish to remove the first matching instance, you can query to find the first item and just remove that:

如果你想删除第一个匹配的实例,你可以查询找到第一项并删除它:

var item = dic.First(kvp => kvp.Value == value);

dic.Remove(item.Key);

Note:The ToList()call is necessary to copy the values to a new collection. If the call is not made, the loop will be modifying the collection it is iterating over, causing an exception to be thrown on the next attempt to iterate after the first value is removed.

注:ToList()调用是必要的值复制到一个新的集合。如果未进行调用,循环将修改它正在迭代的集合,导致在删除第一个值后下次尝试迭代时抛出异常。

回答by Xinus

Loop through the dictionary to find the index and then remove it.

遍历字典以查找索引,然后将其删除。

回答by Amy B

Dictionary<string, string> source
//
//functional programming - do not modify state - only create new state
Dictionary<string, string> result = source
  .Where(kvp => string.Compare(kvp.Value, "two", true) != 0)
  .ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
//
// or you could modify state
List<string> keys = source
  .Where(kvp => string.Compare(kvp.Value, "two", true) == 0)
  .Select(kvp => kvp.Key)
  .ToList();

foreach(string theKey in keys)
{
  source.Remove(theKey);
}

回答by Jahongir Murtozayev

In my case I use this

在我的情况下,我使用这个

  var key=dict.FirstOrDefault(m => m.Value == s).Key;
            dict.Remove(key);

回答by ezolotko

Here is a method you can use:

这是您可以使用的方法:

    public static void RemoveAllByValue<K, V>(this Dictionary<K, V> dictionary, V value)
    {
        foreach (var key in dictionary.Where(
                kvp => EqualityComparer<V>.Default.Equals(kvp.Value, value)).
                Select(x => x.Key).ToArray())
            dictionary.Remove(key);
    }

回答by shoushouleb86

You can use the following as extension method

您可以使用以下作为扩展方法

 public static void RemoveByValue<T,T1>(this Dictionary<T,T1> src , T1 Value)
    {
        foreach (var item in src.Where(kvp => kvp.Value.Equals( Value)).ToList())
        {
            src.Remove(item.Key);
        }
    }