PocoListToDataTable Method
The PocoListToDataTable extension method creates a new IDataTable in an
IDataSet and populates it from a list of POCO items.
It is a convenience method that combines two common steps:
CopyFromPocoList.
This method is typically used when you already have a list of in-memory objects (for example, returned
from a repository, API, or EF Core query) and want to expose them to UI or other POCO DataSet workflows
through a metadata-driven IDataTable.
/// <summary>
/// Creates data table and populates its rows from POCO items
/// </summary>
/// <typeparam name="T">POCO item type</typeparam>
/// <param name="dataSet">Data set</param>
/// <param name="tableName">Table name</param>
/// <param name="pocoItems">POCO items</param>
/// <param name="listOfColumnMetadata">List of column metadata</param>
/// <returns>Created table</returns>
public static IDataTable PocoListToDataTable<T>(this IDataSet dataSet, string tableName, IList<T> pocoItems, List<IColumnMetadata> listOfColumnMetadata)
Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions.dll
dataSet.AddNewTable(tableName, listOfColumnMetadata).
dataTable.CopyFromPocoList(pocoItems).
IDataTable instance.
Assuming that there is IEmploymentType interface:
/// <summary>
/// Defines employment type functionality
/// </summary>
public interface IEmploymentType
{
#region Public Properties
/// <summary>
/// Gets or sets code
/// </summary>
string Code
{
get; set;
}
/// <summary>
/// Gets or sets description
/// </summary>
string? Description
{
get; set;
}
/// <summary>
/// Gets or sets identifier
/// </summary>
int Id
{
get; set;
}
#endregion
}
Assuming that there is an EmploymentType class, which implements IEmploymentType interface:
/// <summary>
/// Provides employment type functionality
/// </summary>
public class EmploymentType : IEmploymentType
{
#region Public Properties
/// <summary>
/// Gets or sets code
/// IEmploymentType interface implementation
/// </summary>
public string Code
{
get; set;
} = default!;
/// <summary>
/// Gets or sets description
/// IEmploymentType interface implementation
/// </summary>
public string? Description
{
get; set;
}
/// <summary>
/// Gets or sets identifier
/// IEmploymentType interface implementation
/// </summary>
public int Id
{
get; set;
}
#endregion
}
// 1. Create an empty data set
IDataSet dataSet = DataSetFactory.CreateDataSet();
// 2. Prepare column metadata (data table schema)
List<ColumnMetadata> listOfColumnMetadata = new List<IColumnMetadata>();
ColumnMetadata columnMetadata = new ColumnMetadata();
columnMetadata.ColumnName = "Id";
columnMetadata.DataType = DataTypeNames.INT32;
columnMetadata.IsNullable = false;
columnMetadata.IsPrimaryKey = true;
listOfColumnMetadata.Add(columnMetadata);
columnMetadata = new ColumnMetadata();
columnMetadata.ColumnName = "Code";
columnMetadata.DataType = DataTypeNames.STRING;
listOfColumnMetadata.Add(columnMetadata);
columnMetadata = new ColumnMetadata();
columnMetadata.ColumnName = "Description";
columnMetadata.DataType = DataTypeNames.STRING;
listOfColumnMetadata.Add(columnMetadata);
// 3. Prepare list of employment types
IList<IEmploymentType> employmentTypes = new List<IEmploymentType>();
IEmploymentType employmentType = new EmploymentType();
employmentType.Id = 1;
employmentType.Code = "ET01";
employmentType.Description = "Full Time";
employmentTypes.Add(employmentType);
employmentType = new EmploymentType();
employmentType.Id = 1;
employmentType.Code = "ET02";
employmentType.Description = "Part Time";
employmentTypes.Add(employmentType);
// 4. Create table and populate it from the POCO list
IDataTable employmentTypeDataTable = dataSet.PocoListToDataTable("EmploymentType", employmentTypes, listOfColumnMetadata);
CopyFromPocoList.
Ensure that your column names and POCO property names (or configured mapping conventions)
align with the expectations of that method.
CopyFromPocoList directly instead of creating a new table.
AddNewTable.
Table of Content POCO DataSet DataSet 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.