Good day.
Closest post I saw was this post.
I have a datatable with the following info:
ID COL1 COL2 COL3
10 ABC Town Dog
20 AAA Town Dog
30 BBB Town Cat
40 CCC City Cat
50 DDD City Pig
I would like to clear out the columns with similar value, so that only the first instance of each remains. The column to filter is user-input, that it can produce:
ID COL1 COL2 COL3 ID COL1 COL2 COL3
10 ABC Town Dog 10 ABC Town Dog
20 AAA Dog 20 AAA Town
30 BBB Cat OR 30 BBB Town Cat
40 CCC City Cat 40 CCC City
50 DDD Pig 50 DDD City Pig
So far, I have a working code, but it performs slowly.
foreach (string strListVal in lstUniqueString) //contains the unique values
{
foreach (DataRow drTableTraverse in dt.Rows) //match the string vs. all rows
{
if (drTableTraverse[strColumnName].ToString() == strListVal && bClearedFirst == false)
{
bClearedFirst = true; //flag for the first instance
continue; //skip the first instance, then clear the remaining
}
else if (drTableTraverse[strColName].ToString() == strListVal)
{
drTableTraverse[strColumnName] = "";
}
}
}
Is there an faster way to achieve the same result? Without using linq, if ever.