How to get distinct rows from a DataTable

[DATATABLE].DefaultView.ToTable(true, FIELDS);

Very simple :)
Earlier to avoid duplicate values, I used a separate function which loop through the datatable to avoid the duplicates. But now it is not required

public static DataTable SelectDistinct(DataTable sourceTable, string fieldName)


DataTable dt = new DataTable();

dt.Columns.Add(fieldName, sourceTable.Columns[fieldName].DataType);

object lastValue = null;

foreach (DataRow dr in sourceTable.Select("", fieldName))


if (lastValue == null

!(compareValues(lastValue, dr[fieldName])))


lastValue = dr[fieldName];

dt.Rows.Add(new object[] { lastValue });



return dt;



Popular posts from this blog

മലയാളത്തില്‍ മൊത്തം എത്ര അക്ഷരങ്ങള്‍ ഉണ്ട്?

Convert Number To Words in SQL Sever

Crystal reports load report failed: Could not load file or assembly CrystalDecisions.Web, Version=10.2.3600.0