How to Delete Duplicate Data from DataTable with Particular Conditions

How to delete duplicate data from DataTable with particular conditions.

Step by Step Process:

Note - Please delete Step Line and execute direct code.

Step 1: First get the value of table from loop which is coming to Database:

for (ii = dt.Rows.Count - 1; ii >= 0; ii--)
{
c = int.Parse(dt.Rows[0]["cityid"].ToString());

Step 2: This is a counter which is count same value in Datatable.

t = 0;

for (int j = 0; j < dt.Rows.Count; j++)

{

if (c == int.Parse(dt.Rows[j]["cityid"].ToString()))

{

t = t + 1;

jump = int.Parse(dt.Rows[j]["cityid"].ToString());

}

}

 
Step 3: Display unique data from datatable with count .

output += "<div class=\"pan_tab\">&rsaquo; &nbsp;<a href='SearchwithLocation.aspx?id=" + dt.Rows[0]["cityid"].ToString() + "&for=" + dt.Rows[0]["PropFor"].ToString() + "'>" + dt.Rows[0]["cityname"].ToString() + "(" + t + ")" + "</a></div>";
output += "<div class=\"PropFor\">&lsaquo; &nbsp;" + dt.Rows[0]["PropFor"].ToString() + "</div>";

Step 4: Take the value for match the duplicate data in rows

match = dt.Rows[0]["cityname"].ToString();

Step 5: And match the condition and delete data from row

foreach (DataRow dr in dt.Rows)

{

if (dr["cityname"].ToString() == match)

{

dr.Delete();

}

}

Step 6: After delete all duplicate data from datatable at disconneted mode .

dt.AcceptChanges();

Step 7: The final state is AcceptChanges() method for new data in datatable.

count = dt.Rows.Count;

ii = count;

output += "</div>";

//For Abudhabi

output += "</div>";

output += "</div>";

//For Tab2

output += "<div id=tab2 class=\"tab_content\">";

output += "<div class=\"pantool\">";

//For sale start

output += "<div id=propheading><u>Ajman Properties For Sale and Rent</u>&nbsp;</div>";

output += "<div class=\"pan_tab1\">"

}