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

Remove Duplicates from Rows/List/Column Values

By , 4 May 2013
 

Introduction

This article will show code to avoid duplicates from a Generic List.

Background

This is used to remove the dupliates rows from list items:

Method 1:

This will return distinct column values based on your datatable columns. Which is used to filter.

Also this will used to "Select" particular column in a datatable to display in grdiview with AutoGeneratedColumn="True" Mode

Using Code:

DataTable tableWithOnlySelectedColumns = new DataView(DataTableName).ToTable(distinct, ColumnNames);

example 1:

public void BindGridView(){

string str[] = {"Dept","EmpFName","EmpLName","Salary","City"};

GridView1.DataSource = GetSelectedColumns(dv.ToTable(), str, true);

GridView1.DataBind();

}

public DataTable GetSelectedColumns(DataTable dtTemp, string[] ColumnNames, bool distinct)
{
DataTable tableWithOnlySelectedColumns = new DataView(dtTemp).ToTable(distinct, ColumnNames);
return tableWithOnlySelectedColumns;
}

Method 2:

List<int> ProductCode = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };

IEnumerable<int> ProductCode = ProductCode.Distinct();

Method 3 : Using Lamda Expression

List <ListName> listObject =  listname.GroupBy(x=> x.ColumnName).Select(y=> y.First()).TOList();

List<ClsPubRepayDetails> repayDetails1 = ((List<ClsPubRepayDetails>)DeSeriliaze (byteLobs));
List<ClsPubRepayDetails> repayDetails = repayDetails1.GroupBy(x => x.InstallmentNo).Select(y => y.First()).ToList();

License

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

About the Author

senthilmsc04
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberSperneder Patrick6 May '12 - 7:14 
GeneralMy vote of 1memberjfriedman5 May '12 - 8:59 
GeneralThoughts [modified]memberPIEBALDconsult5 May '12 - 4:35 
GeneralRe: Thoughtsmemberjfriedman7 May '12 - 3:13 
SuggestionWhat about...memberFréderic Cordier5 May '12 - 4:21 
GeneralRe: What about...memberjfriedman5 May '12 - 9:01 
GeneralRe: What about...memberFréderic Cordier6 May '12 - 21:06 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 4 May 2013
Article Copyright 2012 by senthilmsc04
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid