Question

I have two Lists that I'm trying to compare. What I need to accomplish is essentially to remove the items that are duplicated between the two lists and only keep the objects that are different. Right now, I'm inserting the non-duplicate data into a new list.

The data I'm using here...

LIST1 ("b",2) ("c",3)

LIST2 ("a",1) ("b",2) ("c",3) ("d",4)

NEWLIST ("a",1) ("d",4)

Here's what I have so far...

My object:

public class TestClass
{
    protected string teststring;
    protected int testint;

    public string TestString
    {
        get { return teststring; }
        set { teststring = value; }
    }
    public int TestInt
    {
        get { return testint; }
        set { testint = value; }
    }

    public TestClass() { }
}

My compare logic:

private static List<TestClass> CompareCodes(List<TestClass> list1, List<TestClass> list2)
{
    List<TestClass> newList = new List<TestClass>();
    foreach (TestClass s in list2)
    {
        if (list1.Contains(s) == false)
        {
            newList.Add(s);
        }
    }        

    if (newList.Count != 0)
        return newList;
    else
        return null;
}

The new list will be used to insert data into a database table. If it's null, no action will be taken. I'm using .NET 2.0 in this app (it's an enhancement to an older app), so I can't use LINQ. So is there any other way to make this work that I'm missing? Or is there a better way to do this? I haven't been able to find anything (maybe just not looking hard enough) to accomplish what I'm trying to do.

Thanks in advance!

Was it helpful?

Solution

So you're almost there, but you'll need to override the Equals method on your class to make it work:

public class TestClass
{
    public override bool Equals(object y)
    {
        TestClass newY = y as TestClass;
        if (newY == null) { return false; }

        return newY.TestString == this.TestString &&
            newY.TestInt == this.TestInt;
    }

    public override int GetHashCode()
    {
        unchecked // Overflow is fine, just wrap
        {
            int hash = 17;

            // Suitable nullity checks etc, of course :)
            hash = hash * 23 + this.TestInt.GetHashCode();
            hash = hash * 23 + this.TestString == null ?
                0 :
                this.TestString.GetHashCode();
            return hash;
        }
    }
}

Use Jon Skeet's answer here to implement your hash code.

OTHER TIPS

In the code that you have provided you are only keeping the items of list2 that are not in list1. but how about the items that are in list1 but not in list2 ? and since you mention

What I need to accomplish is essentially to remove the items that are duplicated between the two lists and only keep the objects that are different

This code belows returns a new list with the items that are unique in both lists

private static List<TestClass> CompareCodes(List<TestClass> list1, List<TestClass> list2)
{
    List<TestClass> newList ;

    newList = new List<TestClass>();

    //All the items in list1 that are not in list2 are added
    foreach (TestClass s in list1)
    {
        if ( ! list2.Contains(s))
        {
            newList.Add(s);
        }
    }        

    //All the items in list2 that are not in list1 are added
    foreach (TestClass s in list2)
    {
        if ( ! list1.Contains(s))
        {
            newList.Add(s);
        }
    }        

    return newList;
}

And in your class

public class TestClass implements IEquatable
{
    protected string teststring;
    protected int testint;

    public string TestString
    {
        get { return teststring; }
        set { teststring = value; }
    }
    public int TestInt
    {
        get { return testint; }
        set { testint = value; }
    }
    public override bool Equals(object y)
    {
        TestClass newY = y as TestClass;
        if (newY == null) { return false; }
        return newY.TestString == this.TestString &&
            newY.TestInt == this.TestInt;
    }
    public override int GetHashCode()
    {
        // use this example or implement some hash code logic
        return this.TestInt.GetHashCode() ;
    }
    public TestClass() { }
}
private static List<TestClass> CompareCodes(List<TestClass> list1,
    List<TestClass> list2)
{
    List<TestClass> newList = new List<TestClass>();
    bool found = false; 
    foreach (TestClass s in list2)
    {
        foreach (TestClass t in list1)
        {   
           //let's say that teststring is your object id / key 
            if(s.TestString==t.TestString )
            { 
                found = true; 
                break; 
            }
        }

        if(!found)         
            newList.Add(s);               

       found=false;  
   }
  // do the something for the List1 
    foreach (TestClass s in list1)
    {
        foreach (TestClass t in list2)
        {   
           //let's say that teststring is your object id / key 
            if(s.TestString==t.TestString )
            { 
                found = true; 
                break; 
            }
        }

        if(!found)         
            newList.Add(s);               

       found=false;  
     }



   if (newList != null)
       return newList;
   else
       return null;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top