BuildPrimaryKeyIndex Method
The BuildPrimaryKeyIndex method builds an in-memory primary key index of rows in a table using the table's
primary key values. The returned dictionary maps a compiled primary-key string to the corresponding data row.
This method is primarily used by merge logic (for example, when matching rows between two tables), and it is especially useful for composite primary keys.
/// <summary>
/// </summary>
/// </summary>
/// <param name="dataTable">Data table</param>
/// <param name="primaryKeyColumnNames">Primary key column names</param>
/// <returns>Built primary key index</returns>
public static Dictionary<string, IDataRow> BuildPrimaryKeyIndex(this IDataTable? dataTable, List<string> primaryKeyColumnNames)
Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions.dll
StringComparer.Ordinal for key comparison (case-sensitive).
dataTable is null, returns an empty dictionary.
dataTable.Rows and computes a key for each row by calling
dataRow.CompilePrimaryKeyValue(primaryKeyColumnNames).
The exact key format is determined by CompilePrimaryKeyValue:
Length of the primary key field value + "#" + primary key field value + "|" +
length of the primary key field value + "#" + primary key field value + "|" + ...
This format allows unambiguous concatenation for composite keys (for example, key parts "12" and "3"
do not collide with "1" and "23").
The following example builds a row primary key index for a refreshed table and uses it to look up matching rows from a current table during a merge-like comparison:
// 1. Create an empty data set and a table
IDataSet dataSet = DataSetFactory.CreateDataSet();
IDataTable employeeTable = dataSet.AddNewTable("Employee");
employeeTable.AddColumn("Id", DataTypeNames.INT32);
employeeTable.AddColumn("FirstName", DataTypeNames.STRING);
employeeTable.AddColumn("LastName", DataTypeNames.STRING);
// 2. Add several rows
IDataRow employeeDataRow1 = employeeTable.AddNewRow();
employeeDataRow1["Id"] = 1;
employeeDataRow1["FirstName"] = "John";
employeeDataRow1["LastName"] = "Doe";
IDataRow employeeDataRow2 = employeeTable.AddNewRow();
employeeDataRow2["Id"] = 2;
employeeDataRow2["FirstName"] = "Sara";
employeeDataRow2["LastName"] = "Gor";
// 4. Build primary key index for refreshed rows
Dictionary<string, IDataRow> employeeTablePrimaryKeyIndex = employeeTable.BuildPrimaryKeyIndex(employeeTable.PrimaryKeys);
// 5. Get data rows by its indexes
IDataRow firstDataRow = employeeTablePrimaryKeyIndex["1#1"];
IDataRow secondDataRow = employeeTablePrimaryKeyIndex["1#2"];
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.