Olivarsham
Olivarsham

Reputation: 1731

Finding already existing value in Key Value pair

I am storing a string and int value in Key value pair.

var list = new List<KeyValuePair<string, int>>();

While adding i need to check if string(Key) already exists in list, if exists i need to add it to Value instead of adding new key.
How to check and add?

Upvotes: 25

Views: 96204

Answers (6)

WATYF
WATYF

Reputation: 429

For anyone who has to use a List (which was the case for me, since it does things Dictionary doesn't), you can just use a lambda expression to see if the List contains the Key:

list.Any(l => l.Key == checkForKey);

Upvotes: 8

D J
D J

Reputation: 7028

For sure, dictionary is preferable in your case. You can not modify the Value of KeyValue<string,int> class as it is Immutable.

But even if you still want to use List<KeyValuePair<string, int>>();. You can use IEqualityComparer<KeyValuePair<string, int>>. Code will be like.

public class KeyComparer : IEqualityComparer<KeyValuePair<string, int>>
{

    public bool Equals(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
    {
        return x.Key.Equals(y.Key);
    }

    public int GetHashCode(KeyValuePair<string, int> obj)
    {
        return obj.Key.GetHashCode();
    }
}

And use it in Contains like

var list = new List<KeyValuePair<string, int>>();
        string checkKey = "my string";
        if (list.Contains(new KeyValuePair<string, int>(checkKey, int.MinValue), new KeyComparer()))
        {
            KeyValuePair<string, int> item = list.Find((lItem) => lItem.Key.Equals(checkKey));
            list.Remove(item);
            list.Add(new KeyValuePair<string, int>("checkKey", int.MinValue));// add new value
        }

which does not sounds good way.

hope this info helps..

Upvotes: 4

Ravi Gadag
Ravi Gadag

Reputation: 15861

use dictonary. Dictionary in C# and I suggest you to read this post Dictonary in .net

Dictionary<string, int> dictionary =
        new Dictionary<string, int>();
    dictionary.Add("cat", 2);
    dictionary.Add("dog", 1);
    dictionary.Add("llama", 0);
    dictionary.Add("iguana", -1);

to check. use ContainsKey ContainsKey

if (dictionary.ContainsKey("key"))
    dictionary["key"] = dictionary["key"] + yourValue;

Upvotes: 8

Karthik T
Karthik T

Reputation: 31952

Your needs exactly describe the design of Dictionarys?

Dictionary<string, string> openWith = 
        new Dictionary<string, string>();

// Add some elements to the dictionary. There are no  
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");

// If a key does not exist, setting the indexer for that key 
// adds a new key/value pair.
openWith["doc"] = "winword.exe";

Upvotes: 4

Jhonny
Jhonny

Reputation: 61

If you need use the list,you must foreach the list,and look for the keys. Simplely,you can use hashtable.

Upvotes: 5

Habib
Habib

Reputation: 223267

Instead of List you can use Dictionary and check if it contains key then add the new value to the existing key

int newValue = 10;
Dictionary<string, int> dictionary = new Dictionary<string, int>();
if (dictionary.ContainsKey("key"))
    dictionary["key"] = dictionary["key"] + newValue;

Upvotes: 36

Related Questions