Skip to content

6. GeneratingColumns

Tom Wright edited this page Nov 18, 2018 · 2 revisions

Generating column values

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:

  1. The input type parameter
  2. The output type parameter
  3. A function delegate
  4. A string representing the new column's name
  5. The column(s) in which to project on

Basic usage

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).

  1. 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;
  1. 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 float and the output type is int

  2. Because there is no class to reflect on, the user must provide a column name. Here the name will be OrbitalPeriod (Years)

  3. Lastly, the user must provide the column(s) that will represent the input parameters. These can be obtained via the Columns indexing. For demonstration, our column will be the OrbitalPeriod, 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                   |
+---------+-----------------+---------------+-----------------------+

Clone this wiki locally