CopyFromPoco Method
The CopyFromPoco method copies values from a POCO object into an IDataRow by reading
all public instance properties that have a getter. Each readable property value is assigned to the data row
field with the same name as the property.
This method does not validate that the POCO structure matches the row schema. For predictable results, ensure
that the row contains fields for the POCO properties (for example, by creating the table from an interface using
AddNewTableFromPocoInterface extension method of DataSet and creating the row from the table
columns using AddNewRow extension method of DataTable).
/// <summary>
/// Copies public readable properties from a POCO into a data row.
/// Matching between property names and existing row keys is case-insensitive.
/// If a matching key exists (ignoring case), the existing key is updated to avoid duplicates.
/// If no matching key exists, a new key is added using the property name.
/// </summary>
/// <typeparam name="T">POCO type.</typeparam>
/// <param name="dataRow">Target data row.</param>
/// <param name="poco">Source POCO instance.</param>
public static void CopyFromPoco<T>(this IDataRow? dataRow, T poco)
Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions.dll
Let’s assume there is an interface that defines the employment type:
internal interface IEmploymentType
{
string Code { get; set; }
string? Description { get; set; }
int Id { get; set; }
}
And a class that implements the interface:
internal class EmploymentType
{
public string Code { get; set; }
public string? Description { get; set; }
public int Id { get; set; }
}
Import data from an EmploymentType object into a row:
// 1. Create data set
IDataSet dataSet = DataSetFactory.CreateDataSet();
// 2. Create EmploymentType data table from POCO interface and add a new row to it
IDataTable employmentTypeDataTable = dataSet.AddNewTableFromPocoInterface("EmploymentType", typeof(IEmploymentType));
IDataRow employmentTypeDataRow = employmentTypeDataTable.AddNewRow();
// 3. Create POCO object
EmploymentType employmentType = new EmploymentType();
employmentType.Id = 1;
employmentType.Code = "ET01";
employmentType.Description = "Full Time";
// 4. Copy values into the row
employmentTypeDataRow.CopyFromPoco
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.