Articles → ADO.NET → Get Distinct Rows From Datatable C#

Get Distinct Rows From Datatable C#






Create Data Table




DataTable table = new DataTable();
table.Columns.Add("Name", typeof(System.String));
table.Columns.Add("Gender", typeof(System.String));
table.Columns.Add("City", typeof(System.String));

DataRow row = table.NewRow();
row["Name"] = "Person 1";
row["Gender"] = "Male";
row["City"] = "Delhi";
table.Rows.Add(row);

row = table.NewRow();
row["Name"] = "Person 1";
row["Gender"] = "Male";
row["City"] = "Delhi";
table.Rows.Add(row);

row = table.NewRow();
row["Name"] = "Person 2";
row["Gender"] = "Female";
row["City"] = "Mumbai";
table.Rows.Add(row);




Picture showing the datatable with the duplicate records
Click to Enlarge


Find Distinct Records




DataTable distinct = table.DefaultView.ToTable(true, "Name", "Gender", "City");




Picture showing the datatable with the distinct records
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Monday, December 21, 2015

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250