Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

how to run Distinct query on DataTable?

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
prdeshpande
Asked:
prdeshpande
  • 4
5 Solutions
 
DhaestCommented:
0
 
DhaestCommented:
HOW TO: Implement a DataSet SELECT DISTINCT Helper Class
http://support.microsoft.com/kb/326176
0
 
DhaestCommented:
0
 
DhaestCommented:
0
 
Sudhakar PulivarthiProject Lead - EngineeringCommented:
Hi

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now