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);
Click to Enlarge
Find Distinct Records
DataTable distinct = table.DefaultView.ToTable(true, "Name", "Gender", "City");
Click to Enlarge