1
votes

I have a DataTable with data loaded from an external source, one of the column is an int. I'd like to add a column to the DataTable, containing a description of that int. I have a Dictionary, or in some cases something like:

Pair<int,string> mapping_values[] = { 
   new Pair<String, int>("start", 3),
   new Pair<String, int>("end", 6),
   ... etc.
};

The DataTable contains a "status_value", an integer which maps to the int in mapping_values

DataTable tbl = ...;
tbl.Columns.add("Status Text",typeof(string));

Now, I'd like to fill the values of this new Status Text column with the string from mapping_values where tbl["status_value"] matches the integer (3 or 6 in this case) and fill in "start" or "end". Can linq help me here, or something else ?

1

1 Answers

1
votes

You'll need to manually populate the column in a loop.

To do that, you should put your mapping in a dictionary.

For example:

var mapping = mapping_values.ToDictionary(p => p.Value1, p => p.Value2);
foreach(DataRow row in table.Rows) 
    row["Status Text"] = mapping[row.Field<int>("Status Value")];