Home >Backend Development >C++ >How to Eliminate Duplicate Rows from a DataTable?
Removing Duplicate Rows from a DataTable
Data tables often contain duplicate entries, impacting data integrity. This article demonstrates a simple and efficient method to eliminate these duplicates using the DataTable
object's DefaultView
.
Solution:
To create a DataTable (distinctTable
) containing only unique rows from an existing DataTable (dtEmp
), use this concise code snippet:
<code class="language-csharp">DataTable distinctTable = dtEmp.DefaultView.ToTable(true);</code>
Explanation:
This approach leverages the DefaultView
property, which generates a DataView
representing the dtEmp
table. The ToTable(true)
method then converts this DataView
back into a DataTable
, but crucially, the true
argument instructs it to exclude duplicate rows. The resulting distinctTable
contains only unique rows from the original. This provides a clean and effective way to ensure data uniqueness.
The above is the detailed content of How to Eliminate Duplicate Rows from a DataTable?. For more information, please follow other related articles on the PHP Chinese website!