Best way to introduce this lookup table in C #

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

Name    Range   Multiplier

Active  10-20   0.5

What are you offering?

I will need to search for the range and get the multiplier. I also need to search by name.

UPDATE There will be 10-15 lines in total. Range is an integer date type.

+3
source share
4 answers

In fact, you have two lookup tables: by name and by range. There are several ways to represent them in memory, depending on how large the table is.

Basically, most likely suitable for searching by name - this is a dictionary:

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

. , , , , , . , , (: ).

(Dictionary<int, double>), , int List<double>, "" .

, : , , , .

+4

. Name, RangeLow, RangeHigh Multiplier. ( ), LINQ :

from r in LookupTable
where r.RangeLow <= x && r.RangeHigh >= x
select r.Multiplier;
+3

. , , , , ? , , , , , : (), . , , , - , .

+2

I would implement this using a DataTable, considering that there is no pressing reason to use a different data type. DataTable.Select works great for starting a search by name or range. You are losing some performance using a DataTable for this, but with 10-15 records this will go a long way.

+1
source

Source: https://habr.com/ru/post/1704915/


All Articles