Dictionary question

  • Thread starter Thread starter CSharper
  • Start date Start date
C

CSharper

I have created a Dictionary and I ran into a situation where my key
may not be exact match, if the key contains partial key, I need to
return the value associated with it. What is the best way to do it?

I was going to see if there is a method like ContainsKeys but all I
found is Contains on values.

Thanks.
 
I have created a Dictionary and I ran into a situation where my key
may not be exact match, if the key contains partial key, I need to
return the value associated with it. What is the best way to do it?

I was going to see if there is a method like ContainsKeys but all I
found is Contains on values.

If you want to do some sort of fuzzy key matching, implement
IEqualityComparer and pass that to your dictionary.

Jon
 
I have created a Dictionary and I ran into a situation where my key
may not be exact match, if the key contains partial key, I need to
return the value associated with it. What is the best way to do it?

I was going to see if there is a method like ContainsKeys but all I
found is Contains on values.

Thanks.

you could do the following

class MyDictionary : IDictionary<TK, TV>
{

private Dictionary<TK, TV> _myDictionary;

public MyDictionary()
{
_myDictionary = new Dictionary<TK, TV>();
}

#region IDictionary<TK,TV> Members

public void Add(TK key, TV value)
{
_myDictionary.Add(key, value);
}

public bool ContainsKey(TK key)
{
if (_myDictionary.ContainsKey(key))
return true;

//else do you partial key match here
foreach (TK key in _myDictionary.Keys)
{
//if(Something matches)
//return true;
}

return false;
}
DOT DOT DOT
 
you could do the following

class MyDictionary : IDictionary<TK, TV>
{

private Dictionary<TK, TV> _myDictionary;

public MyDictionary()
{
_myDictionary = new Dictionary<TK, TV>();
}

#region IDictionary<TK,TV> Members

public void Add(TK key, TV value)
{
_myDictionary.Add(key, value);
}

public bool ContainsKey(TK key)
{
if (_myDictionary.ContainsKey(key))
return true;

//else do you partial key match here
foreach (TK key in _myDictionary.Keys)
{
//if(Something matches)
//return true;
}

return false;
}
DOT DOT DOT

MyDictionary<TK,TV> : IDictionary<TK, TV>
 
you could do the following

    class MyDictionary : IDictionary<TK, TV>
    {

        private Dictionary<TK, TV> _myDictionary;

        public MyDictionary()
        {
            _myDictionary = new Dictionary<TK, TV>();
        }

        #region IDictionary<TK,TV> Members

        public void Add(TK key, TV value)
        {
            _myDictionary.Add(key, value);
        }

        public bool ContainsKey(TK key)
        {
            if (_myDictionary.ContainsKey(key))
                return true;

            //else do you partial key match here
            foreach (TK key in _myDictionary.Keys)
            {
                //if(Something matches)
                //return true;
            }

            return false;
        }
DOT DOT DOT

Thanks both for the answer. I think I got the picture.
 
CSharper said:
I have created a Dictionary and I ran into a situation where my key
may not be exact match, if the key contains partial key, I need to
return the value associated with it. What is the best way to do it?

I was going to see if there is a method like ContainsKeys but all I
found is Contains on values.

Dictionary is hash based. That is not good for finding partial
keys. You would need to iterate over all keys. A tree structure
is better for partial keys.

Arne
 
Back
Top