Select DISTINCT records based on specified fields for DataTable






4.95/5 (21 votes)
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