gnihar
asked on
C# remove duplicates in a List
Hi experts,
I am pasting the method to remove duplicate String objects from any given List of string.
My question is, can you modify this method to be able to remove DataRow objects that contain duplicate DataColumn values (from the List of DataRows):
Additionally, replace the value of another datarow column in the original list with the value from last spotted duplicate.
ie. in pseudo code
if (DataRow row.DataColumn["column"]= /*duplicate*/ )
{
/*then do not add entire row in new list*/
/*but take row.DataColumn["column2"] value and insert that value instead of the original ["column2"] already present in the final list*/
}
I am pasting the method to remove duplicate String objects from any given List of string.
My question is, can you modify this method to be able to remove DataRow objects that contain duplicate DataColumn values (from the List of DataRows):
static List<string> removeDuplicates(List<string> inputList)
{
Dictionary<string, int> uniqueStore = new Dictionary<string, int>();
List<string> finalList = new List<string>();
foreach (string currValue in inputList)
{
if (!uniqueStore.ContainsKey(currValue))
{
uniqueStore.Add(currValue, 0);
finalList.Add(currValue);
}
}
return finalList;
}
Additionally, replace the value of another datarow column in the original list with the value from last spotted duplicate.
ie. in pseudo code
if (DataRow row.DataColumn["column"]= /*duplicate*/ )
{
/*then do not add entire row in new list*/
/*but take row.DataColumn["column2"] value and insert that value instead of the original ["column2"] already present in the final list*/
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
I probably wasn't specific enough.
In the first answer, it seems that Uniquify function takes all column values from a single row:
and out of that value, gives a dictionary an unique value - hash code which is then used to uniquely identify that row in a new collection.
If I am not mistaken, this will look for all differences in all columns of a specific row, and if precisely 0 differences are found, will then add that row in a new collection.
But in my example I have to :
ie.
iterate through datatables' rows
{
if impurecollection.row["colu mn name1"] == purecollection.row["column name1] (regardless of other column values in the same row, they can be duplicates)
then
do not add the whole row in new collection
but
take impurecollection.row["column name 2"] and insert it's value instead of purecollection. row["column name 2"] where impurecollection.row["colu mn name1"] == purecollection.row["column name1"]
and if impurecollection.row["colu mn name1"] != purecollection.row["column name1]
then add that row from impurecollection into purecollection unchanged
}
Hope that clarifies things a bit.
I probably wasn't specific enough.
In the first answer, it seems that Uniquify function takes all column values from a single row:
for (int i = 0; i < dr.ItemArray.Length; i++)
{
concat += dr[i].ToString();
}
and out of that value, gives a dictionary an unique value - hash code which is then used to uniquely identify that row in a new collection.
If I am not mistaken, this will look for all differences in all columns of a specific row, and if precisely 0 differences are found, will then add that row in a new collection.
But in my example I have to :
look in only specific columns if it's a duplicate
if it is, then I do not insert the row in a collection, but take another column value out of the 'duplicate' row and overwrite the new collections' row's column value with the same name with the value from 'duplicate' row's column.
ie.
iterate through datatables' rows
{
if impurecollection.row["colu
then
do not add the whole row in new collection
but
take impurecollection.row["column name 2"] and insert it's value instead of purecollection. row["column name 2"] where impurecollection.row["colu
and if impurecollection.row["colu
then add that row from impurecollection into purecollection unchanged
}
Hope that clarifies things a bit.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi gnihar,
Thankyou for accepting my solution. I'm afraid I had missed the end of your post which is why I didn't follow-up with that solution. I'm glad my solution was adaptable however.
Thankyou for accepting my solution. I'm afraid I had missed the end of your post which is why I didn't follow-up with that solution. I'm glad my solution was adaptable however.
ASKER
see my last post
static List<string> removeDuplicates(List<stri
{
return inputList.Distinct().ToLis
}
static List<DataRow> removeDuplicates(List<Data
{
return rows.Distinct(new MyRowComnparer()).ToList()
}
public class MyRowComnparer : IEqualityComparer<DataRow>
{
public bool Equals(DataRow x, DataRow y)
{
// put your logic here
}
public int GetHashCode(DataRow obj)
{
// put your logic here
}
}