根据值删除字典中的项

时间:2022-03-30 19:44:55

I have a 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仅仅表示true/false,而不是该条目的索引或键。

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

6 个解决方案

#1


103  

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()调用对于将值复制到新集合是必要的。如果没有进行调用,循环将修改它正在迭代的集合,导致在删除第一个值之后的下一次迭代尝试时抛出异常。

#2


6  

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

#3


2  

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

遍历字典查找索引,然后删除它。

#4


1  

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

#5


0  

In my case I use this

在我的例子中,我用这个。

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

#6


-1  

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

#1


103  

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()调用对于将值复制到新集合是必要的。如果没有进行调用,循环将修改它正在迭代的集合,导致在删除第一个值之后的下一次迭代尝试时抛出异常。

#2


6  

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

#3


2  

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

遍历字典查找索引,然后删除它。

#4


1  

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

#5


0  

In my case I use this

在我的例子中,我用这个。

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

#6


-1  

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

相关文章