Class DataTableExtensions
Provides extension methods for enhanced DataTable operations in the Xperiflow system.
Namespace: Workspace.XBR.Xperiflow.Utilities.AdoDataTable.Extensions
Assembly: Xperiflow.dll
public static class DataTableExtensions
Examples
Basic DataTable extension usage:
// Create a sample DataTable
var dataTable = new DataTable();
dataTable.Columns.Add("ID", typeof(string));
dataTable.Columns.Add("Amount", typeof(string));
dataTable.Rows.Add("123", "45.67");
dataTable.Rows.Add("456", "89.12");
// Convert column types
dataTable.ConvertColumnType(sessionInfo, "ID", typeof(Guid));
dataTable.ConvertColumnType(sessionInfo, "Amount", typeof(decimal));
// Duplicate a column for backup
dataTable.DuplicateColumn("Amount", "OriginalAmount");
// Export to CSV for debugging
dataTable.LogToCsv(sessionInfo, @"C:\Temp\", "ProcessedData");
Remarks
The Workspace.XBR.Xperiflow.Utilities.AdoDataTable.Extensions.DataTableExtensions class extends the standard System.Data.DataTable
functionality
with commonly needed operations for data processing workflows. These extensions are designed to
simplify data manipulation tasks while maintaining compatibility with the OneStream platform.
Key Extension Categories:
-
Column Type ConversionSafe conversion of column data types with support for nullable types and custom conversion logic
-
Column ManagementDuplication, reordering, and manipulation of DataTable columns
-
Data ExportExport DataTable contents to various formats including CSV and OneStream error logs
-
Debugging SupportEnhanced logging and debugging capabilities for DataTable inspection
Error Handling:
All methods include comprehensive error handling with OneStream integration, ensuring that
errors are properly logged and wrapped in OneStream.Shared.Common.XFException
instances for consistent
error management across the platform.
Performance Considerations:
The extension methods are optimized for performance while maintaining data integrity. Type conversion operations use efficient algorithms and avoid unnecessary data copying where possible.
Methods
ConvertColumnType(DataTable, SessionInfo, string, Type)
Converts the data type of a column in a DataTable to a new data type with support for custom conversion logic.
public static void ConvertColumnType(this DataTable dt, SessionInfo si, string columnName, Type newType)
Remarks
This method provides safe type conversion for DataTable columns with special handling for common conversion scenarios. It preserves the original column position and handles null values appropriately during conversion.
Supported Conversions:
Performance Note:
The method creates a temporary column to avoid data loss during conversion, ensuring that if the conversion fails partway through, the original data remains intact.
Parameters
Type | Name | Description |
---|---|---|
System.Data.DataTable | dt | The DataTable containing the column to convert |
OneStream.Shared.Common.SessionInfo | si | The session information for error logging and context |
System.String | columnName | The name of the column to convert |
System.Type | newType | The new data type to convert the column to |
Exceptions
System.ArgumentException
Thrown when the specified column does not exist in the DataTable
OneStream.Shared.Common.XFException
Thrown when conversion fails or other errors occur during processing
ConvertColumnTypes(DataTable, SessionInfo, Dictionary<string, Type>)
Converts the data types of multiple columns in a DataTable using a dictionary mapping.
public static void ConvertColumnTypes(this DataTable dt, SessionInfo si, Dictionary<string, Type> columnDataTypes)
Remarks
This method provides efficient bulk conversion of multiple columns while preserving the original column order. It uses a temporary column approach to ensure data integrity during the conversion process.
Conversion Process:
Error Handling:
If any conversion fails, the method throws an exception with details about the specific column and value that caused the failure, making debugging easier.
Parameters
Type | Name | Description |
---|---|---|
System.Data.DataTable | dt | The DataTable to modify |
OneStream.Shared.Common.SessionInfo | si | The session information for error logging and context |
System.Collections.Generic.Dictionary<System.String,System.Type> | columnDataTypes | A dictionary mapping column names to their new data types |
Exceptions
System.ArgumentException
Thrown when columnDataTypes is null/empty or a column name is not found
System.InvalidCastException
Thrown when a value cannot be converted to the specified type
OneStream.Shared.Common.XFException
Thrown when other errors occur during processing
DuplicateColumn(DataTable, string, string)
Creates a duplicate of a column in a DataTable with a new name.
public static void DuplicateColumn(this DataTable dt, string columnName, string newColumnName)
Remarks
This method creates an exact copy of a column including its data type, default value, and expression (if any). The duplicated column is added to the end of the column collection by default.
Copied Properties:
Note: The Unique property is not copied to avoid constraint conflicts.
Parameters
Type | Name | Description |
---|---|---|
System.Data.DataTable | dt | The DataTable containing the column to duplicate |
System.String | columnName | The name of the column to duplicate |
System.String | newColumnName | The name for the duplicated column |
Exceptions
System.ArgumentNullException
Thrown when dt is null
System.ArgumentException
Thrown when the source column doesn't exist or the target column name already exists
LogToOneStreamErrorLog(DataTable, SessionInfo, int, string)
Logs the contents of a DataTable to the OneStream error log.
public static void LogToOneStreamErrorLog(this DataTable dt, SessionInfo si, int maxRowsToPrint = -1, string columnSeparator = "|")
Parameters
| Type | Name | Description |
| :------------------------------------ | :---------------- | :-------------------------------------------------------------------------------------- | ------------ |
| System.Data.DataTable
| dt | The DataTable to log. |
| OneStream.Shared.Common.SessionInfo
| si | The SessionInfo object associated with the operation. |
| System.Int32
| maxRowsToPrint | The maximum number of rows to include in the log message. If -1, all rows are included. |
| System.String
| columnSeparator | The separator to use between columns in the log message. Default is "\ | " character. |
LogToCsv(DataTable, SessionInfo, string, string)
Logs the contents of a DataTable to a CSV file.
public static void LogToCsv(this DataTable dt, SessionInfo si, string folderPath = "C:\\Windows\\Temp\\", string fileNameSuffix = "Data")
Parameters
Type | Name | Description |
---|---|---|
System.Data.DataTable | dt | The DataTable to log. |
OneStream.Shared.Common.SessionInfo | si | The SessionInfo object associated with the operation. |
System.String | folderPath | The path of the folder where the CSV file will be saved. Default is "C:\Windows\Temp". |
System.String | fileNameSuffix | The suffix to be added to the file name. Default is "Data". |
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object,System.Object)
System.Object.GetHashCode
System.Object.GetType
System.Object.MemberwiseClone
System.Object.ReferenceEquals(System.Object,System.Object)
System.Object.ToString