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#: Use Pattern Matching for Cleaner Type Checks and Switches

GC.AllocateUninitializedArray — Faster Arrays With a Catch

IndexOf() and LastIndexOf() in C#

Post Views: 815

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 *

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

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

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

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

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

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