Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure to hold 3 columns and lookup for a row fast by any of the columns

We had one hashtable as a readonly reference to a list of values like this:

internal static readonly Hashtable relationcodeAcodeB = new Hashtable
{
    {"149", "23"},
    {"139", "17"}
}

Now we need an structure that can hold 3 values(columns) and look up a value fast by any of the other 2.

Something like this:

internal static readonly Hashtable relationcodeAcodeBcodeC = new Hashtable
{
    {"149", "23", "xx"},
    {"139", "17", "xxx"}
}
string codeB=relationcodeAcodeBcodeC[codeA="149"]["codeB"];
like image 243
VSP Avatar asked Nov 04 '22 22:11

VSP


1 Answers

Say your object has three properties codeA, codeB and codeC, you maintain three hash tables, like so:

Dictionary<string, MyObj> dictA, dictB, dictC;

When creating a new MyObj, you add it to the three dictionaries:

dictA[obj.codeA] = obj;
dictB[obj.codeB] = obj;
dictC[obj.codeC] = obj;

Looking up is very easy. Your example will be coded as dictA["149"].codeB

Keep it all tidy in one big lookup class, of course.

like image 129
zmbq Avatar answered Nov 09 '22 17:11

zmbq