Home > Backend Development > C++ > How to Get Unique Values from a DataTable Column and Store Them in an Array?

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

Patricia Arquette
Release: 2025-01-06 16:34:41
Original
395 people have browsed it

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

Selecting Distinct Rows in a DataTable and Storing into an Array

In this scenario, you have a dataset objds with a table Table1 containing a column ProcessName with repeated values. Your goal is to select only the distinct values from ProcessName and store them in an array.

To achieve this, you can leverage the DataView class in conjunction with the DataTable class. Here's an approach:

// 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();
}
Copy after login

This code will create a DataTable (distinctValues) that contains only the distinct values from the ProcessName column. You can then access the distinct values by iterating over the intUniqId array.

The above is the detailed content of How to Get Unique Values from a DataTable Column and Store Them in an Array?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template