首页 >后端开发 >C++ >如何从数据表列中获取唯一值并将它们存储在数组中?

如何从数据表列中获取唯一值并将它们存储在数组中?

Patricia Arquette
Patricia Arquette原创
2025-01-06 16:34:41461浏览

How to Get Unique Values from a DataTable Column and Store Them in an Array?

选择 DataTable 中的不同行并将其存储到数组中

在这种情况下,您有一个数据集 objds,其表 Table1 包含具有重复值的 ProcessName 列。您的目标是仅选择 ProcessName 中的不同值并将它们存储在数组中。

要实现此目的,您可以结合使用 DataView 类和 DataTable 类。这是一种方法:

// Create a DataView from the table
DataView view = new DataView(objds.Tables[0]);

// Set the Distinct property to true
view.Distinct = true;

// Create a new DataTable with only the distinct rows
DataTable distinctValues = view.ToTable(true, "ProcessName");

// Create an array to store the distinct values
string[] intUniqId = new string[distinctValues.Rows.Count];

// Populate the array with the distinct ProcessName values
for (int i = 0; i < distinctValues.Rows.Count; i++)
{
    intUniqId[i] = distinctValues.Rows[i]["ProcessName"].ToString();
}

此代码将创建一个 DataTable (distinctValues),其中仅包含 ProcessName 列中的不同值。然后,您可以通过迭代 intUniqId 数组来访问不同的值。

以上是如何从数据表列中获取唯一值并将它们存储在数组中?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn