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 |
| Static |
|
| Static |
| |
| Extension |
| |
Add Rows |
| Extension |
|
| Extension |
| |
| Extension |
| |
| Extension |
| |
Define Columns |
| Extension |
|
| Extension |
|
Supported Property Types
The following property types are supported when generating table schemas from classes or objects:
stringNumeric types (
int,long,double,float,decimal, etc.) and their nullable formsbool/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:
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— IftableNameordataToAddisnull.
Example:
CreateDataTableForClass
Purpose:
Creates an empty DataTable for class type T, with columns defined from the class’s supported properties.
Signature:
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— IfschemaortableNameisnull.
Example:
AddClassPropertiesToDataTable
Purpose:
Adds columns to a DataTable for all supported public properties from class type T.
Signature:
Parameters:
dt(DataTable) — Target table.
Returns:void
Throws:
ArgumentNullException— Ifdtisnull.
Example:
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— Ifdtoroisnull.
Example:
AddRow (from DataRow)
Purpose:
Adds an existing DataRow to a DataTable.
Signature:
Parameters:
dt(DataTable) — Target table.row(DataRow) — Existing row to add.
Returns:DataRow
Throws:
ArgumentNullException— Ifdtorrowisnull.
Example: var newRow = otherTable.Rows; dataTable.AddRow(newRow);
AddRows (from objects)
Purpose:
Adds multiple rows from a collection of objects.
Signature:
Parameters:
dt(DataTable) — Target table.objects(IEnumerable<object>) — Source objects.
Returns:IEnumerable<DataRow> — The newly added rows.
Throws:
ArgumentNullException— Ifdtorobjectsisnull.
Example:
AddRows (from DataRow collection)
Purpose:
Adds multiple existing DataRow instances to the DataTable.
Signature:
Parameters:
dt(DataTable) — Target table.rows(IEnumerable<DataRow>) — Rows to add.
Returns:IEnumerable<DataRow>
Throws:
ArgumentNullException— Ifdtorrowsisnull.
Example:
3. Define Columns
AddColumn
Purpose:
Adds a column with a given name and data type.
Signature:
Parameters:
dt(DataTable) — Target table.columnName(string) — Column name.type(Type) — Data type.
Returns:DataColumn
Throws:
ArgumentNullException— If any argument isnull.
Example:
AddColumnWithLength
Purpose:
Adds a column with an optional maximum string length.
Signature:
Parameters:
dt(DataTable) — Target table.columnName(string) — Column name.type(Type) — Data type.length(int?) — Maximum length (if applicable).
Returns:DataColumn
Throws:
ArgumentNullException— Ifdt,columnName, ortypeisnull.ArgumentOutOfRangeException— Iflengthis less than 1 when specified.
Example: