Solved

how to run Distinct query on DataTable?

Posted on 2011-03-11
5
651 Views
Last Modified: 2012-06-27
Hi, I have created DataTable “dt1” programmatically and enter query results in that datatable “dt1”.
Below is the code:
string strquery = "SELECT Name, Phones FROM Directory";
SqlCommand cmd = new SqlCommand(strquery, connection);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt1 = new DataTable();
da.Fill(dt1);
---------------------------------------------------------------------
Now, I want to run Distinct query on one of the column “Phone” or Name and result should get store in another datatable.

How it can be done?

Regards,  
0
Comment
Question by:prdeshpande
  • 4
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 400 total points
ID: 35107660
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 400 total points
ID: 35107666
HOW TO: Implement a DataSet SELECT DISTINCT Helper Class
http://support.microsoft.com/kb/326176
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 400 total points
ID: 35107671
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 400 total points
ID: 35107673
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 100 total points
ID: 35108374
Hi

// Get the distinct values applied on the column
string[] columns = {"Phone"};
DataTable distinctTable = dataTable.DefaultView.ToTable(true, columns);
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question