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" or "name" or "NAME", etc.. If the field is missing or its value is null, that property is skipped.

Declaration

/// <summary>
/// Creates a new POCO instance from an observable 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="observableDataRow">Source observable data row</param>
/// <returns>New POCO instance</returns>
public static T ToPoco<T>(this IObservableDataRow? observableDataRow) where T : new()

Namespace and Assembly

Namespace: PocoDataSet.ObservableExtensions
Assembly: PocoDataSet.ObservableExtensions.dll

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 observable data set
IObservableDataSet observableDataSet = new ObservableDataSet();

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

IObservableDataRow employmentTypeObservableDataRow = employmentTypeObservableDataTable.AddNewRow();
employmentTypeObservableDataRow["Id"] = 1;
employmentTypeObservableDataRow["Code"] = "ET01";
employmentTypeObservableDataRow["Description"] = "Full Time";

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

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

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

 

Table of Content POCO DataSet Observable Data Row 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.