IT Community - Software Programming, Web Development and Technical Support

How to get distinct records from datatable

This is a discussion on How to get distinct records from datatable within the C# Programming forums, part of the Software Development category; Hi, I am having a datatable that consists of many records and name is duplicated many times in this table. ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-05-2008, 02:42 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default How to get distinct records from datatable

Hi,

I am having a datatable that consists of many records and name is duplicated many times in this table. I want to get the distinct records using name column.
Can anyone give me an idea to do this?
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-05-2008, 02:47 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: How to get distinct records from datatable

Hi,

This is the simple way to do this.

consider, sourceDataTable consists of duplicate records.

DataSet ds = new DataSet();
ds.Tables.Add(sourceDataTable);
DataTable distinctDataTable = SelectDistinct("DistinctTable", sourceDataTable, "name");



public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
{
DataTable dt = new DataTable(TableName);
dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);

object LastValue = null;
foreach (DataRow dr in SourceTable.Select("", FieldName))
{
if (LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])))
{
LastValue = dr[FieldName];
dt.Rows.Add(new object[] { LastValue });
}
}

return dt;
}
private bool ColumnEqual(object A, object B)
{

if (A == DBNull.Value && B == DBNull.Value)
return true;
if (A == DBNull.Value || B == DBNull.Value)
return false;
return (A.Equals(B));

}
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort Datatable oxygen C# Programming 2 04-22-2008 10:25 PM
Delete records from multiple table at a time Falcon Database Support 7 04-07-2008 01:12 AM
How will get distinct values from datatable Mramesh C# Programming 5 02-14-2008 08:51 PM
Convert Dataview to Datatable it.wily C# Programming 2 01-23-2008 02:51 AM
How to remove duplicate records from a table? with out using distinct key? KiruthikaSambandam Database Support 6 08-06-2007 10:16 PM


All times are GMT -7. The time now is 02:42 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0