Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

How to select distinct rows in a datatable in C#

- 10.03.20 | 24.03.26 - ErcanOPAK

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 to true to act like a SQL SELECT 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.

Related posts:

C#: Throttling Concurrent Operations with SemaphoreSlim

C#: Use ValueTask for Async Methods That Often Return Synchronously

Request Body Can Only Be Read Once

Post Views: 805

Post navigation

ignoring any ‘bin’ directory on a git project
How to run code if not in Debug Mode in C#

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (448)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com