ToPoco Method

Overview

The ToPoco method creates a new POCO object of type T and copies values from a data row into the POCO’s public writable properties. The created POCO object is a detached copy: it is not linked to the underlying row, and changes made to the POCO are not reflected in the row.

Property values are matched by name: a POCO property named Name reads from the row field "Name". If the field is missing or its value is null, that property is skipped.

Declaration

/// <summary>
/// Creates a new POCO instance from a data row by copying row values into writable properties.
/// Matching between property names and row keys is case-insensitive.
/// </summary>
/// <typeparam name="T">POCO type.</typeparam>
/// <param name="dataRow">Source data row.</param>
/// <returns>New POCO instance.</returns>
public static T ToPoco<T>(this IDataRow? dataRow) where T : new()

Namespace and Assembly

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

Behavior

Usage Example

Let’s assume the following POCO type:

internal class EmploymentType
{
  public int Id { get; set; }
  public string Code { get; set; }
  public string? Description { get; set; }
}

Copy data from a row into a POCO:

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

// 2. Create EmploymentType table and add a new row
IDataTable employmentTypeDataTable = dataSet.AddNewTable("EmploymentType");
employmentTypeDataTable.AddColumn("Id", DataTypeNames.INT32);
employmentTypeDataTable.AddColumn("Code", DataTypeNames.STRING);
employmentTypeDataTable.AddColumn("Description", DataTypeNames.STRING);

IDataRow employmentTypeDataRow = employmentTypeDataTable.AddNewRow();
employmentTypeDataRow["Id"] = 1;
employmentTypeDataRow["Code"] = "ET01";
employmentTypeDataRow["Description"] = "Full Time";

// 3. Convert row to POCO
EmploymentType employmentTypePoco = employmentTypeDataRow.ToPoco<EmploymentType>();

// 4. Change Code in POCO
employmentTypePoco.Code = "ET03";

// 5. Observe that changes are NOT propagated to data row (expected "ET01")
string? rowCode = employmentTypeDataRow["Code"] as string;

 

Table of Content POCO DataSet DataRow 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.