Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Tip/Trick

Select DISTINCT records based on specified fields for DataTable

Rate me:
Please Sign up or sign in to vote.
4.95/5 (25 votes)
2 Feb 2011CPOL 197.7K   13   13
Select DISTINCT records based on specified fields for DataTable
This will help you only when you have to filter distinct records based on specified fields.
e.g. I have a table called UserDetail which contains the following fields:

UserID | Name | Mobile | Email | City | State

Now I want to only display distinct records with Name, City and State, then you can use:

C#
string[] TobeDistinct = {"Name","City","State"};
DataTable dtDistinct = GetDistinctRecords(DTwithDuplicate, TobeDistinct);

//Following function will return Distinct records for Name, City and State column.
public static DataTable GetDistinctRecords(DataTable dt, string[] Columns)
   {
       DataTable dtUniqRecords = new DataTable();
       dtUniqRecords = dt.DefaultView.ToTable(true, Columns);
       return dtUniqRecords;
   }


Thanks,
Imdadhusen

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Infostretch Ahmedabad-Gujarat
India India
Aspiring for a challenging carrier wherein I can learn, grow, expand and share my existing knowledge in meaningful and coherent way.

sunaSaRa Imdadhusen


AWARDS:

  1. 2nd Best Mobile Article of January 2015
  2. 3rd Best Web Dev Article of May 2014
  3. 2nd Best Asp.Net article of MAY 2011
  4. 1st Best Asp.Net article of SEP 2010


Read More Articles...

Comments and Discussions

 
QuestionDo we even need an article for this? Pin
Daniel Wilianto20-Nov-18 16:06
Daniel Wilianto20-Nov-18 16:06 
Question[My vote of 1] Not interesting Pin
Wrangly6-Feb-18 21:22
Wrangly6-Feb-18 21:22 
QuestionThanks Pin
Member 1355302630-Jan-18 20:05
Member 1355302630-Jan-18 20:05 
GeneralMy vote of 5 Pin
DeadlyEmbrace6-Aug-13 0:37
DeadlyEmbrace6-Aug-13 0:37 
GeneralRe: My vote of 5 Pin
Sunasara Imdadhusen22-Apr-14 3:51
professionalSunasara Imdadhusen22-Apr-14 3:51 
GeneralMy vote of 5 Pin
Savalia Manoj M3-Jul-13 1:56
Savalia Manoj M3-Jul-13 1:56 
GeneralRe: My vote of 5 Pin
Sunasara Imdadhusen22-Apr-14 3:51
professionalSunasara Imdadhusen22-Apr-14 3:51 
QuestionGreat Pin
Axel C-manager3-Oct-12 4:37
Axel C-manager3-Oct-12 4:37 
AnswerRe: Great Pin
Sunasara Imdadhusen22-Apr-14 3:52
professionalSunasara Imdadhusen22-Apr-14 3:52 
GeneralReason for my vote of 1 xzcz Pin
sagar makde16-Sep-11 21:06
sagar makde16-Sep-11 21:06 
GeneralRe: Reason for my vote of 1xzcz Pin
Sunasara Imdadhusen22-Apr-14 3:53
professionalSunasara Imdadhusen22-Apr-14 3:53 
GeneralReason for my vote of 5 Awesome one Pin
NIRMAL UPADHYAY21-Jun-11 18:33
NIRMAL UPADHYAY21-Jun-11 18:33 
GeneralRe: Reason for my vote of 5Awesome one Pin
Sunasara Imdadhusen22-Apr-14 3:53
professionalSunasara Imdadhusen22-Apr-14 3:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.