-
Notifications
You must be signed in to change notification settings - Fork 19
6. GeneratingColumns
By using the AddGeneratedColumn method, columns can be generated dynamically using preexisting values. Similar to that of the Select method on a list. This method and its various overloads generally accept the following:
- The input type parameter
- The output type parameter
- A function delegate
- A string representing the new column's name
- The column(s) in which to project on
In this example, we will project on the OrbitalPeriod property from our table of the type Planet. It is worth noting that we are not projecting on the instances of Planet, but the columns itself. Leaving the original objects unchanged and adding an additional column to the table.
After the following command:
var table = Table<Planet>.Create(Planets);The resulting table will be as such:
+---------+-----------------+---------------+
| Name | DistanceFromSun | OrbitalPeriod |
+---------+-----------------+---------------+
| Mercury | 57909227 | 88 |
| Venus | 108209475 | 225 |
| Earth | 149598262 | 365.24 |
| Mars | 227943824 | 693.5 |
| Jupiter | 778340821 | 4343.5 |
| Saturn | 1426666422 | 10767.5 |
| Uranus | 2870658186 | 30660 |
| Neptune | 4498396441 | 60152 |
+---------+-----------------+---------------+
To present additional data to the user while maintaining the simplicity of our class Planet, we can use the AddGeneratedColumn. We will be converting OrbitalPeriod from representing partial days to whole years. To do so, we must first write a function to convert from days to years (for simplicity's sake, we will assume there is always 365 days in a year).
- Since C# 7, function delegates could either use the local function style or lambda style. Our delegate will use the lambda style and look like such:
(days) => (int) days / 365;-
Our lambda method is implicitly typed, because of such we will have to manually pass the expected input and output parameters. In this case, the input type is
floatand the output type isint -
Because there is no class to reflect on, the user must provide a column name. Here the name will be
OrbitalPeriod (Years) -
Lastly, the user must provide the column(s) that will represent the input parameters. These can be obtained via the
Columnsindexing. For demonstration, our column will be theOrbitalPeriod, obtained using the following:
table.Columns["OrbitalPeriod"]Once the expression created it will resemble the following:
table.Columns.AddGeneratedColumn<float, int>(
(days) => (int) days / 365,
"OrbitalPeriod (Years)",
table.Columns["OrbitalPeriod"]);After we add our new column OrbitalPeriod (Years), our table will be the following:
+---------+-----------------+---------------+-----------------------+
| Name | DistanceFromSun | OrbitalPeriod | OrbitalPeriod (Years) |
+---------+-----------------+---------------+-----------------------+
| Mercury | 57909227 | 88 | 0 |
| Venus | 108209475 | 225 | 0 |
| Earth | 149598262 | 365.24 | 1 |
| Mars | 227943824 | 693.5 | 1 |
| Jupiter | 778340821 | 4343.5 | 11 |
| Saturn | 1426666422 | 10767.5 | 29 |
| Uranus | 2870658186 | 30660 | 84 |
| Neptune | 4498396441 | 60152 | 164 |
+---------+-----------------+---------------+-----------------------+