Click here to Skip to main content
Click here to Skip to main content

Select DISTINCT records based on specified fields for DataTable

, 2 Feb 2011
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)

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. 1st Best Asp.Net article of SEP 2010
  2. 2nd Best Asp.Net article of MAY 2011
 
Read More Articles...
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinmemberCuteD34th6-Aug-13 0:37 
GeneralRe: My vote of 5 PinpremiumSunasara Imdadhusen22-Apr-14 3:51 
GeneralMy vote of 5 PinmemberSavalia Manoj M3-Jul-13 1:56 
GeneralRe: My vote of 5 PinpremiumSunasara Imdadhusen22-Apr-14 3:51 
QuestionGreat PinmemberAxel C-manager3-Oct-12 4:37 
AnswerRe: Great PinpremiumSunasara Imdadhusen22-Apr-14 3:52 
GeneralReason for my vote of 1 xzcz Pinmembersagar makde16-Sep-11 21:06 
GeneralRe: Reason for my vote of 1xzcz PinpremiumSunasara Imdadhusen22-Apr-14 3:53 
GeneralReason for my vote of 5 Awesome one PinmemberNIRMAL UPADHYAY21-Jun-11 18:33 
GeneralRe: Reason for my vote of 5Awesome one PinpremiumSunasara Imdadhusen22-Apr-14 3: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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 2 Feb 2011
Article Copyright 2011 by Sunasara Imdadhusen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid