Click here to Skip to main content
11,437,713 members (33,453 online)
Click here to Skip to main content

Tagged as

Select DISTINCT records based on specified fields for DataTable

, 2 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Sunasara Imdadhusen
Software Developer (Senior) 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...
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 Pin
CuteD34th6-Aug-13 1:37
memberCuteD34th6-Aug-13 1:37 
GeneralRe: My vote of 5 Pin
Sunasara Imdadhusen22-Apr-14 4:51
professionalSunasara Imdadhusen22-Apr-14 4:51 
GeneralMy vote of 5 Pin
Savalia Manoj M3-Jul-13 2:56
memberSavalia Manoj M3-Jul-13 2:56 
GeneralRe: My vote of 5 Pin
Sunasara Imdadhusen22-Apr-14 4:51
professionalSunasara Imdadhusen22-Apr-14 4:51 
QuestionGreat Pin
Axel C-manager3-Oct-12 5:37
memberAxel C-manager3-Oct-12 5:37 
AnswerRe: Great Pin
Sunasara Imdadhusen22-Apr-14 4:52
professionalSunasara Imdadhusen22-Apr-14 4:52 
GeneralReason for my vote of 1 xzcz Pin
sagar makde16-Sep-11 22:06
membersagar makde16-Sep-11 22:06 
GeneralRe: Reason for my vote of 1xzcz Pin
Sunasara Imdadhusen22-Apr-14 4:53
professionalSunasara Imdadhusen22-Apr-14 4:53 
GeneralReason for my vote of 5 Awesome one Pin
NIRMAL UPADHYAY21-Jun-11 19:33
memberNIRMAL UPADHYAY21-Jun-11 19:33 
GeneralRe: Reason for my vote of 5Awesome one Pin
Sunasara Imdadhusen22-Apr-14 4:53
professionalSunasara Imdadhusen22-Apr-14 4:53 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150506.1 | Last Updated 2 Feb 2011
Article Copyright 2011 by Sunasara Imdadhusen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid