CopyFromPocoList Method

Overview

The CopyFromPocoList extension method populates a table by creating one new row per POCO instance in a list.

For each POCO item, the method calls IDataTable.AddNewRow(), copies POCO properties into the row via CopyFromPoco, and then calls AcceptChanges() to mark the row as Unchanged.

Declaration

/// <summary>
/// Copies data from POCO list into data table by adding to table a new row for every item in the list
/// </summary>
/// <typeparam name="T">POCO item type</typeparam>
/// <param name="dataTable">Data table</param>
/// <param name="pocoItems">POCO items</param>
public static void CopyFromPocoList<T>(this IDataTable? dataTable, IList<T>? pocoItems)

Namespace and Assembly

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

Behavior

Usage Example

using System.Collections.Generic;
using PocoDataSet.Data;
using PocoDataSet.Extensions;

public sealed class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

// 1) Build a table schema
DataTable employees = new DataTable();
employees.TableName = "Employee";
employees.AddColumn("Id", "Int32", isNullable: false, isPrimaryKey: true);
employees.AddColumn("FirstName", "String", isNullable: true);
employees.AddColumn("LastName", "String", isNullable: true);

// 2) Prepare POCOs
List<Employee> list = new List<Employee>();
list.Add(new Employee { Id = 1, FirstName = "Ada", LastName = "Lovelace" });
list.Add(new Employee { Id = 2, FirstName = "Alan", LastName = "Turing" });

// 3) Load them into the table
employees.CopyFromPocoList(list);

// Result: employees.Rows.Count == 2 and rows are Unchanged (AcceptChanges() was called).

Notes

If you want inserted rows to remain in Added state (for example, to produce a changeset later), you can copy the implementation and remove the AcceptChanges() call.

AddNewRow() ensures the special __ClientKey column exists and assigns a new correlation key.

 

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.