VSP
VSP

Reputation: 2393

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"];

Upvotes: 5

Views: 2305

Answers (1)

zmbq
zmbq

Reputation: 39013

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.

Upvotes: 3

Related Questions