Home >Backend Development >C++ >How can I efficiently remove duplicate rows from a DataTable in C#?
Eliminating Duplicate Rows in C# DataTables
Data redundancy is a frequent problem in data handling. This article shows how to efficiently remove duplicate rows from a C# DataTable, maintaining data integrity.
A straightforward approach uses the DefaultView
property. This provides a dynamic view of the DataTable, allowing data manipulation without altering the original table.
The following code snippet generates a new DataTable containing only unique rows:
<code class="language-csharp">DataTable uniqueTable = dtEmp.DefaultView.ToTable(true);</code>
Here, dtEmp
is your original DataTable with potential duplicates. The ToTable(true)
method creates uniqueTable
, containing only unique rows. The true
argument ensures duplicate rows are omitted.
This method leverages the filtering capabilities of DefaultView
to efficiently remove duplicates during table creation. The resulting uniqueTable
contains a clean dataset.
The above is the detailed content of How can I efficiently remove duplicate rows from a DataTable in C#?. For more information, please follow other related articles on the PHP Chinese website!