The Scenario: You’ve loaded a massive DataTable from an Excel file, a CSV, or a legacy database. Now you realize it contains duplicate entries, and you need a unique list based on one or more columns—without writing complex foreach loops or heavy LINQ queries.
The Pro Solution: The built-in DefaultView.ToTable() method is the most efficient “one-liner” to filter unique rows directly within the ADO.NET framework.
🚀 The “One-Liner” Unique Filter
To get a new DataTable containing only unique values from a specific column, use this syntax:
// Returns a new DataTable with unique "CategoryName" values DataTable uniqueCategories = sourceTable.DefaultView.ToTable(true, "CategoryName");
🔍 Deep Dive: Understanding the Parameters
The ToTable method is surprisingly powerful. Here is exactly what is happening under the hood:
- First Parameter (
bool isDistinct): Set this totrueto act like a SQLSELECT DISTINCT. It compares the values of the columns you specify and removes duplicates. - Second Parameter (
params string[] columnNames): This defines your “Target Schema.” Only the columns you list here will exist in the resulting DataTable.
💡 Advanced: Multi-Column Distinction
Need to find unique combinations of multiple columns? You can pass an array of column names. This is perfect for generating unique “City + Country” pairs, for example:
// Get unique combinations of both City and Country
string[] columnsToMap = { "City", "Country" };
DataTable uniqueLocations = dataTable.DefaultView.ToTable(true, columnsToMap);
💎 Pro Tip: ToTable vs. LINQ
While LINQ’s .Distinct() is popular, ToTable is often faster for raw DataTables because it operates directly on the DataView layer without the overhead of creating anonymous objects or requiring DataRowComparer.
Bonus: The resulting DataTable preserves the column data types perfectly!
Summary
Don’t over-engineer your data filtering. Use DefaultView.ToTable(true, ...) for a clean, readable, and high-performance way to handle duplicate data in your C# backend.
