user1182553
user1182553

Reputation: 53

Check dictionary Key Contains

I have to store two key one value in dictionary. So that i am using like this.

Dictionary<int, Dictionary<int, int>> dicThreatPurgeSummary = new Dictionary<int,Dictionary<int,int>>();

I have added key, key and value.

Dictionary<int, int> innerdict = new Dictionary<int,int>();
innerdict.Add(Month, Count);
dicThreatPurgeSummary.Add(Group, innerdict);

i am able to view value like this

int a = dicThreatPurgeSummary[Group][Month];

I need to update the value for dicThreatPurgeSummary[Group][Month] if it already exists. Please help me to find out this.

Upvotes: 1

Views: 6669

Answers (4)

Rawling
Rawling

Reputation: 50114

Slightly more efficient than two calls to ContainsKey and three dictionary indexings:

Dictionary<int, int> forGroup;
if (dicThreatPurgeSummary.TryGetValue(Group, out forGroup) &&
    forGroup.ContainsKey(Month))
{
    forGroup[Month] = newValue;
}

Upvotes: 4

poke
poke

Reputation: 387677

You can use Dictionary<>.ContainsKey to check if a key exists, so you would do it like this:

if (dicThreatPurgeSummary.ContainsKey(Group))
{
    if (dicThreatPurgeSummary[Group].ContainsKey(Month))
    {
        // dicThreatPurgeSummary[Group][Month] exists
    }
}

Note that it might be better to use a two dimensional key instead of cascading two dictionaries. That way you won’t need to care to add a new dictionary every time you use a new key for the first level, and you would have all values in the same dictionary, allowing you to iterate over the values or check for values existance.

You could use a tuple for that:

var dicThreatPurgeSummary = new Dictionary<Tuple<int, int>, int>();
dicThreatPurgeSummary.Add(new Tuple<int, int>(Group, Month), Count);

// accessing the value
int a = dicThreatPurgeSummary[new Tuple<int, int>(Group, Month)];

// checking for existance
if (dicThreatPurgeSummary.ContainsKey(new Tuple<int, int>(Group, Month)))
{
    // ...
}

Using a prettier subtype

(untested)

class IntIntDict<T> : Dictionary<Tuple<int, int>, T>
{
    public T this[int index1, int index2]
    { get { return this[new Tuple(index1, index2)]; } }

    public bool ContainsKey (int index1, int index2)
    {
        return ContainsKey(new Tuple(index1, index2));
    }

    public void Add (int index1, int index2, T value)
    {
         Add(new Tuple(index1, index2), value);
    }
    // ...
}

And then you could just use it like this:

var dicThreatPurgeSummary = new IntIntDict<int>();
dicThreatPurgeSummary.Add(Group, Month, Count);

// accessing the value
int a = dicThreatPurgeSummary[Group, Month];

// checking for existance
if (dicThreatPurgeSummary.ContainsKey(Group, Month))
{
    // ...

}

Upvotes: 0

nakiya
nakiya

Reputation: 14413

Dictionary<int, int> innerDic = null;
bool isGroupPresent = dicThreatPurgeSummary.TryGetValue(Group, out innerDic);
if(isGroupPresent == false)
{
    //No entry for Group.
}
else
{
    int count;
    bool isMonthPresent = innerDic.TryGetValue(Month, out count);
    //Same as above
}

Upvotes: 0

juharr
juharr

Reputation: 32266

This should work

if(dicThreatPurgeSummary.ContainsKey(Group) &&
   dicThreatPurgeSummary[Group].ContainsKey(Month))
{
    dicThreatPurgeSummary[Group][Month] = NewValue;
}

Upvotes: 3

Related Questions