ToDetachedArray Method

Overview

The ToDetachedArray extension method converts all rows of data table into an array of detached interface objects. Each returned object represents a snapshot of the row values at the moment the method is called.

Detached objects are not connected to the data table after creation. Snapshot proxy properties are read-only. Attempting to assign to a property throws NotSupportedException. Reading a property returns the captured value, and subsequent changes to the data table are not reflected in the detached objects.

Declaration

/// <summary>
/// Returns data table as a detached array
/// </summary>
/// <typeparam name="TInterface">The interface type describing the table's row contract.</typeparam>
/// <param name="dataTable">Data table</param>
/// <returns>Data table as a detached array</returns>
public static TInterface[] ToDetachedArray<TInterface>(this IDataTable? dataTable) where TInterface : class

/// <summary>
/// Returns data table as a detached array
/// </summary>
/// <typeparam name="TInterface">The interface type describing the table's row contract.</typeparam>
/// <param name="dataTable">Data table</param>
/// <param name="nameMap">Name map</param>
/// <returns>Data table as a detached array</returns>
public static TInterface[] ToDetachedArray<TInterface>(this IDataTable? dataTable, IDictionary<string, string> nameMap) where TInterface : class

Namespace and Assembly

Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions.dll

Exceptions

Behavior

Usage Example

Assuming that there is an interface IEmploymentType, which defines employment type functionality:

/// <summary>
// Defines employment type functionality
/// </summary>
public interface IEmploymentType
{
    int Id { get; set; }
    string Code { get; set; }
    string? Description { get; set; }
}

// 1. Create data set
IDataSet dataSet = DataSetFactory.CreateDataSet();

// 2. Add employment type table to data set
IDataTable employmentTypeDataTable = dataSet.AddNewTable("EmploymentType");
employmentTypeDataTable.AddColumn("Id", DataTypeNames.INT32);
employmentTypeDataTable.AddColumn("Code", DataTypeNames.STRING);
employmentTypeDataTable.AddColumn("Description", DataTypeNames.STRING);

// 3. Add several rows to employment type table
IDataRow employmentTypeDataRow1 = employmentTypeDataTable.AddNewRow();
employmentTypeDataRow1["Id"] = 1;
employmentTypeDataRow1["Code"] = "ET01";
employmentTypeDataRow1["Description"] = "Full Time";

IDataRow employmentTypeDataRow2 = employmentTypeDataTable.AddNewRow();
employmentTypeDataRow2["Id"] = 2;
employmentTypeDataRow2["Code"] = "ET02";
employmentTypeDataRow2["Description"] = "Part Time";

// 4. Call ToDetachedArray method to get an array of snapshot (read-only) proxies
IEmploymentType[] employmentTypes = employmentTypeDataTable.ToDetachedArray<IEmploymentType>();

Notes

 

Table of Content POCO DataSet DataTable Extensions Group DataTable Members

 


Business Process Programming in .Net
© 2004–2026 Laskarzhevsky Software Inc.
Unless otherwise noted, the content of this website is licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).
Code examples are provided under the MIT License.
You are free to share and adapt the material provided that appropriate credit is given and any modifications are clearly indicated.
The information provided on this website is for educational purposes only.
The author and publisher make no warranties regarding the completeness or suitability of the information and are not responsible for any damages resulting from its use.