CopyToPoco Method
The CopyToPoco method copies values from an observable data row into an existing POCO instance by
assigning the POCO’s public writable properties.
Property values are matched by name. For a POCO property named Name, the method looks for a row field
with key "Name" first (case-sensitive), and if not found it tries a case-insensitive match against the
row’s existing keys. If a matching field is missing, the property is left unchanged.
If a matching field exists but its value is DBNull.Value, the method treats it as null.
Values are assigned using basic conversion rules for common types (including enums, Guid, and
DateTime). If conversion is not possible, the original value is assigned as-is.
If observableDataRow or pocoInstance is null, the method returns without making changes.
/// <summary>
/// Copies values from an observable data row into a POCO instance by assigning writable properties.
/// Matching between property names and existing row keys is case-insensitive.
/// </summary>
/// <typeparam name="T">POCO type</typeparam>
/// <param name="observableDataRow">Source observable data row</param>
/// <param name="pocoInstance">Target POCO instance to populate</param>
public static void CopyToPoco<T>(this IObservableDataRow? observableDataRow, T? pocoInstance)
Namespace: PocoDataSet.ObservableExtensions
Assembly: PocoDataSet.ObservableExtensions.dll
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 an existing POCO instance:
// 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. Create POCO instance and populate it from the row
EmploymentType employmentTypePoco = new EmploymentType();
employmentTypeObservableDataRow.CopyToPoco(employmentTypePoco);
// 4. employmentTypePoco now contains row values
int id = employmentTypePoco.Id; // 1
string code = employmentTypePoco.Code; // "ET01"
string? desc = employmentTypePoco.Description; // "Full Time"
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.