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


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250