Bragi Docs Help

BragiCodeExtensions

BragiCodeExtensions is a static helper class that provides extension methods and static functions to simplify building and manipulating DataTable objects in Bragi’s code components.

These utilities:

  • Reduce boilerplate when working with DataTable.

  • Maintain consistency in table creation, row insertion, and column definition.

  • Support common primitive and nullable types.

Quick Reference Table

Category

Method

Static/Extension

Returns

Create & Define Tables

CreateDataTableFromEnumerable<T>

Static

DataTable

CreateDataTableForClass<T>

Static

DataTable

AddClassPropertiesToDataTable<T>

Extension

void

Add Rows

AddRow(this DataTable, object)

Extension

DataRow

AddRow(this DataTable, DataRow)

Extension

DataRow

AddRows(this DataTable, IEnumerable<object>)

Extension

IEnumerable<DataRow>

AddRows(this DataTable, IEnumerable<DataRow>)

Extension

IEnumerable<DataRow>

Define Columns

AddColumn(this DataTable, string, Type)

Extension

DataColumn

AddColumnWithLength(this DataTable, string, Type, int?)

Extension

DataColumn

Supported Property Types

The following property types are supported when generating table schemas from classes or objects:

  • string

  • Numeric types (int, long, double, float, decimal, etc.) and their nullable forms

  • bool/bool?

  • DateTime/DateTime?

  • char/char?

1. Create and Define Tables from Classes

CreateDataTableFromEnumerable

Purpose:
Creates a DataTable populated with rows from a collection of class instances.

Signature:

public static DataTable CreateDataTableFromEnumerable<T>( string tableName, IEnumerable<T> dataToAdd)

Parameters:

  • tableName (string) — Name of the resulting table (may include schema prefix).

  • dataToAdd (IEnumerable<T>) — Collection of objects to populate the table.

Returns:
DataTable — New table containing rows mapped from the provided collection.

Throws:

  • ArgumentNullException — If tableName or dataToAdd is null.

Example:

var employees = new List<Employee> { new Employee { Id = 1, Name = "Alice", StartDate = DateTime.UtcNow }, new Employee { Id = 2, Name = "Bob", StartDate = DateTime.UtcNow } }; DataTable dt = BragiCodeExtensions.CreateDataTableFromEnumerable("dbo.Employees", employees);

CreateDataTableForClass

Purpose:
Creates an empty DataTable for class type T, with columns defined from the class’s supported properties.

Signature:

public static DataTable CreateDataTableForClass<T>( string schema, string tableName)

Parameters:

  • schema (string) — Database schema name (e.g., "dbo").

  • tableName (string) — Name of the table.

Returns:
DataTable — An empty table with the correct column schema.

Throws:

  • ArgumentNullException — If schema or tableName is null.

Example:

DataTable dt = BragiCodeExtensions.CreateDataTableForClass<Employee>("dbo", "Employees");

AddClassPropertiesToDataTable

Purpose:
Adds columns to a DataTable for all supported public properties from class type T.

Signature:

public static void AddClassPropertiesToDataTable<T>(this DataTable dt)

Parameters:

  • dt (DataTable) — Target table.

Returns:
void

Throws:

  • ArgumentNullException — If dt is null.

Example:

myDataTable.AddClassPropertiesToDataTable<Employee>();

2. Add Rows

AddRow (from object properties)

Purpose:
Adds a row by mapping matching property values from an object to the DataTable columns.

Signature: public static DataRow AddRow(this DataTable dt, object o)

Parameters:

  • dt (DataTable) — Target table.

  • o (object) — Source object; property names must match columns.

Returns:
DataRow — The newly added row.

Throws:

  • ArgumentNullException — If dt or o is null.

Example:

dataTable.AddRow(new Employee { Id = 3, Name = "Charlie" });

AddRow (from DataRow)

Purpose:
Adds an existing DataRow to a DataTable.

Signature:

public static DataRow AddRow(this DataTable dt, DataRow row)

Parameters:

  • dt (DataTable) — Target table.

  • row (DataRow) — Existing row to add.

Returns:
DataRow

Throws:

  • ArgumentNullException — If dt or row is null.

Example: var newRow = otherTable.Rows; dataTable.AddRow(newRow);

AddRows (from objects)

Purpose:
Adds multiple rows from a collection of objects.

Signature:

public static IEnumerable<DataRow> AddRows(this DataTable dt, IEnumerable<object> objects)

Parameters:

  • dt (DataTable) — Target table.

  • objects (IEnumerable<object>) — Source objects.

Returns:
IEnumerable<DataRow> — The newly added rows.

Throws:

  • ArgumentNullException — If dt or objects is null.

Example:

dataTable.AddRows(myListOfEmployees);

AddRows (from DataRow collection)

Purpose:
Adds multiple existing DataRow instances to the DataTable.

Signature:

public static IEnumerable<DataRow> AddRows(this DataTable dt, IEnumerable<DataRow> rows)

Parameters:

  • dt (DataTable) — Target table.

  • rows (IEnumerable<DataRow>) — Rows to add.

Returns:
IEnumerable<DataRow>

Throws:

  • ArgumentNullException — If dt or rows is null.

Example:

dataTable.AddRows(otherTable.AsEnumerable());

3. Define Columns

AddColumn

Purpose:
Adds a column with a given name and data type.

Signature:

public static DataColumn AddColumn(this DataTable dt, string columnName, Type type)

Parameters:

  • dt (DataTable) — Target table.

  • columnName (string) — Column name.

  • type (Type) — Data type.

Returns:
DataColumn

Throws:

  • ArgumentNullException — If any argument is null.

Example:

dataTable.AddColumn("MyColumn", typeof(string));

AddColumnWithLength

Purpose:
Adds a column with an optional maximum string length.

Signature:

public static DataColumn AddColumnWithLength( this DataTable dt, string columnName, Type type, int? length)

Parameters:

  • dt (DataTable) — Target table.

  • columnName (string) — Column name.

  • type (Type) — Data type.

  • length (int?) — Maximum length (if applicable).

Returns:
DataColumn

Throws:

  • ArgumentNullException — If dt, columnName, or type is null.

  • ArgumentOutOfRangeException — If length is less than 1 when specified.

Example:

dataTable.AddColumnWithLength("MyText", typeof(string), 255);
09 March 2026