Check if an item exist in the dictionary and remov

2019-07-10 09:07发布

This question already has an answer here:

The question should be clear from the title itself. I need to check if an item exist in the dictionary and remove it from the dictionary in C#. The only catch is that i have to do this using only the value item and not the key.

The declaration is as below:

IDictionary<string, myCustomClassObject> clients = new IDictionary<string, myCustomClassObject>();

Now i fill in the dictionary by:

clients["key"] = myCustomClassObject1;

Now how can i find and remove this item myCustomClassObject1 from my Dictionary. I only want to use the value item and not the key

Is this doabale...if so please guide... regards

Edit: Thank you all....got valuable comments...probably have some thinking to do ...thanks

5条回答
我命由我不由天
2楼-- · 2019-07-10 09:32

It depends on how you need it to perform. If you can accept O(N) performance, you could just do something like:

foreach(var pair in clients) {
    if(pair.Value == expected) {
        clients.Remove(pair.Key);
        break;
    }
}

However, if you need faster you would need two dictionaries - one the reverse of the other (i.e. keyed by the instances). So when adding, you would do:

clientsByKey.Add(key, value);
clientsByValue.Add(value, key);

so you can do (to remove-by-value):

string key;
if(clientsByValue.TryGetValue(value, out key)) {
    clientsByValue.Remove(value);
    clientsByKey.Remove(key);
}

or similarly (to remove-by-key):

Foo value;
if(clientsByKey.TryGetValue(key, out value)) {
    clientsByValue.Remove(value);
    clientsByKey.Remove(key);
}
查看更多
相关推荐>>
3楼-- · 2019-07-10 09:42

Use, Following will remove only first matching value

client newClient = new client();

foreach(KeyValuePair<string, client> client in clients) {
    if(client.value.equals(newClient)) {
        clients.remove(client.key);
        break;
    }       
}

Or if you want to remove all matching values,

foreach(var client in clients.Where(kvp => kvp.Value == newClient).ToList()) {
    clients.Remove(client.Key);
}
查看更多
爷的心禁止访问
4楼-- · 2019-07-10 09:43

It's not very efficient to search a dictionary by it's values. However, you can use Linq to find all entries with a given value.

IEnumerable<KeyValuePair<string, myCustomClassObject>> pairs = clients
    .Where(entry => entry.Value.Equals(myCustomClassObject1)).ToList();
foreach (KeyValuePair<string, myCustomClassObject> kv in pairs)
    clients.Remove(kv.Key);
查看更多
Lonely孤独者°
5楼-- · 2019-07-10 09:43

This should do it. It removes all clients having a given value.

while (clients.ContainsValue(myCustomClassObject1))
    clients.Remove(clients.Where(x => x.Value == myCustomClassObject1).FirstOrDefault().Key);

Or create a new dictionary without the values you want removed

clients = clients.Where(x => x.Value != myCustomClassObject1).ToDictionary(k => k.Key, v => v.Value);
查看更多
爷、活的狠高调
6楼-- · 2019-07-10 09:44

If the collection only contains one item with the value to be removed then you can use one of the other answers here, which will work just fine.

However, if your collection can have multiple items with the same value then you need to be careful.

You cannot modify a collection while iterating over it, so you will need to find the keys of all the items that you want to remove in one loop and put them in a list, and then iterate over that list in a separate loop to delete the items.

For example:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    class Program
    {
        void run()
        {
            var dict = new Dictionary<string, int>
            {
                {"Key1", 1}, 
                {"Key2", 2}, 
                {"Key3", 3}, 
                {"Key4", 2}, 
                {"Key5", 4}
            };

            int valueToRemove = 2;

            var keysToRemove = (from element in dict
                                where element.Value == valueToRemove
                                select element.Key).ToList();

            foreach (var key in keysToRemove)
                dict.Remove(key);

            foreach (var element in dict)
                Console.WriteLine("Key = {0}, Value = {1}", element.Key, element.Value);
        }

        static void Main(string[] args)
        {
            new Program().run();
        }
    }
}
查看更多
登录 后发表回答