Search code examples
c#hashtable

C# - Partial Search in Hashtables


Can we check for a part of string in hashtable key? For example, our key is

key = cardnumber +","+ tokennumber

Eg, key would be then

key = 1234,7463

I know have a function called containskey() but do we have something that could partially check for a string in a hashtable key?

For example, if we could search

if(key.contains(tokennumber))
{
//do something
}

Solution

  • You can do it but the lookup won't be as fast.

    You can cast the Keys Collection to of type string and then perform basic Linq on it to get the Keys that you want.

    Example:

    var listOfKeys = hashTable.Keys.Cast<string>().Where(x => x.Contains(tokennumber)).ToList();
    

    after this, you can loop over the Keys and perform the operation that you want.