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. ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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... |
| Sponsored Links |
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |