TryGetFieldKeyByColumnName Method
The TryGetFieldKeyByColumnName method helps resolve the actual field key used inside a
data row when you only know a column name. This is particularly useful when field keys are stored with a specific
casing (for example, "FirstName") but callers may provide a different casing (for example, "firstname").
The method performs a fast exact-match lookup first. If that fails, it scans existing keys using a case-insensitive
comparison and returns the matching key. When no matching key exists, the method returns false.
/// <summary>
/// Tries to get field key by column name.
/// </summary>
/// <param name="dataRow">Data row.</param>
/// <param name="columnName">Column name.</param>
/// <param name="foundFieldKey">Found field key.</param>
/// <returns>True if field key found, otherwise false.</returns>
public static bool TryGetFieldKeyByColumnName(this IDataRow? dataRow, string columnName, out string? foundFieldKey)
Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions.dll
dataRow is null, sets foundFieldKey to null and returns false.
ContainsKey. If found, returns that key as-is.
dataRow.Values.Keys and compares keys with columnName
using StringComparison.OrdinalIgnoreCase.
true and outputs the existing key from the row.
foundFieldKey to columnName and returns false.
(This allows callers to keep a single variable for subsequent access attempts.)
Resolve the correct field key when the caller uses a different casing:
// 1. Create an empty data set
IDataSet dataSet = DataSetFactory.CreateDataSet();
// 2. Create Employee table and add some columns
IDataTable employeeTable = dataSet.AddNewTable("Employee");
employeeTable.AddColumn("Id", DataTypeNames.INT32);
employeeTable.AddColumn("FirstName", DataTypeNames.STRING);
// 3. Add a new row with a specific field key casing (e.g., "FirstName")
IDataRow employeeRow = employeeTable.AddNewRow();
employeeRow["Id"] = 1;
employeeRow["FirstName"] = "Sara";
// 4. Try to find filed key by a different casing (e.g., "firstname") and observe that field key is found
bool found = employeeRow.TryGetFieldKeyByColumnName("firstname", out string? fieldKey);
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.