Home >Backend Development >C++ >How can I efficiently remove duplicate rows from a DataTable in C#?

How can I efficiently remove duplicate rows from a DataTable in C#?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-07 17:57:41777browse

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!

Statement:
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