best way to represent this lookup table in c#

boingboing picture boingboing · Mar 17, 2009 · Viewed 10k times · Source

I need to represent a lookup table in C#, here is the basic structure:

Name    Range   Multiplier

Active  10-20   0.5

What do you guys suggest?

I will need to lookup on range and retrieve the multiplier. I will also need to lookup using the name.

UPdate It will have maybe 10-15 rows in total. Range is integer date type.

Answer

Joel Coehoorn picture Joel Coehoorn · Mar 17, 2009

What you actually have is two lookup tables: one by Name and one by Range. There are several ways you can represent these in memory depending on how big the table will get.

The mostly-likely fit for the "by-name" lookup is a dictionary:

var MultiplierByName = new Dictionary<string, double>() { {"Active",.5}, {"Other", 1.0} };

The range is trickier. For that you will probably want to store either just the minimum or the maximum item, depending on how your range works. You may also need to write a function to reduce any given integer to it's corresponding stored key value (hint: use integer division or the mod operator).

From there you can choose another dictionary (Dictionary<int, double>), or if it works out right you could make your reduce function return a sequential int and use a List<double> so that your 'key' just becomes an index.

But like I said: to know for sure what's best we really need to know the scope and nature of the data in the lookup, and the scenario you'll use to access it.