0

Here is some code I'm trying to get working. If an item in one set doesn't match an item in the other set a 0 is added to a list for all items compared. If in the end the list doesn't contain any other values than 0 it means the item from the first set doesn't exist at all in the second set. For some reason or another I keep getting wrong values in the resulting list, so there must be a bug somewhere, it's just that I can't seem to find it.

    public class CompareItem : IComparable
    {
        public string CustId { get; set; }
        public string TechId { get; set; }

        public CompareItem(string custId, string techId)
        {
            CustId = custId;
            TechId = techId;
        }

        public int CompareTo(object obj)
        {
            CompareItem Temp = (CompareItem)obj;
            if (this.CustId != Temp.CustId || this.TechId != Temp.TechId)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
    }

    static void Main(string[] args)
    {
        List<CompareItem> LeftCompareSet = new List<CompareItem>();

        LeftCompareSet1.Add(new CompareItem("0000", "0001"));
        LeftCompareSet1.Add(new CompareItem("0001", "0001"));
        LeftCompareSet1.Add(new CompareItem("0002", "0002"));
        LeftCompareSet1.Add(new CompareItem("0003", "0003"));
        LeftCompareSet1.Add(new CompareItem("0002", "0004"));

        List<CompareItem> RightCompareSet = new List<CompareItem>();

        RightCompareSet1.Add(new CompareItem("0005", "0005"));
        RightCompareSet1.Add(new CompareItem("0004", "0004"));
        RightCompareSet1.Add(new CompareItem("0003", "0003"));
        RightCompareSet1.Add(new CompareItem("0002", "0002"));
        RightCompareSet1.Add(new CompareItem("0006", "0002"));

        int state = 0;

        List<int> tlc = new List<int>();
        List<int> trc = new List<int>();

        foreach (CompareItem lc in LeftCompareSet)
        {
            foreach (CompareItem rc in RightCompareSet)
            {
                state = lc.CompareTo(rc);
                if (state == 0)
                {
                    tlc.Add(0);
                }
                else
                { 
                    tlc.Add(1);
                }
            }

            if (tlc.Contains(1))
            {
                Console.WriteLine("Cust: " + lc.CustId + ", Tech: " + lc.TechId + ", Not missing");
            }
            else
            {
                Console.WriteLine("Cust: " + lc.CustId + ", Tech: " + lc.TechId + ", Missing");
            }
        }

        foreach (CompareItem rc in RightCompareSet)
        {
            foreach (CompareItem lc in LeftCompareSet)
            {
                state = rc.CompareTo(lc);
                if (state == 0)
                {
                    trc.Add(0);
                }
                else
                {
                    trc.Add(1);
                }
            }

            if (trc.Contains(1))
            {
                Console.WriteLine("Cust: " + rc.CustId + ", Tech: " + rc.TechId + ", Not missing");
            }
            else
            {
                Console.WriteLine("Cust: " + rc.CustId + ", Tech: " + rc.TechId + ", Missing");
            }
        }
    }
Willem
  • 81
  • 2
  • 9

3 Answers3

2

Your CompareTo is wrong. It should return 0 is the two objects are the same, -1 if one is smaller than the other and 1 if it's larger. See here

zmbq
  • 38,013
  • 14
  • 101
  • 171
  • Thanks for this. So I change return 1 into return 0 if the objects match, but I have to return something if the objects don't match, and I'm not interested if the properties of the object are larger or smaller. Using 1 for non-matching objects seems to work, except for the last item in the second list, it says it is contained in the first list, which isn't the case. – Willem Apr 25 '12 at 12:13
  • You should override `Equals` and `GetHashCode` of your class instead, it makes more sense. – zmbq Apr 25 '12 at 14:59
0

I think there are several issues in this approach:

  1. the code contains a minor mistake: it must be LeftCompareSet instead of LeftCompareSet1

  2. In CompareTo you miss to check the type of the object, if the method is called with a wrong object you get an exception.

  3. I think what you really need is to implement the Equals method: public override bool Equals(object obj) (do not forget GetHashCode)

  4. Not sure what you really want to achieve, possibly a set operation is more helpful

  5. You didn't write what is wrong with the results, if I execute your code, I get the following result:

    • Cust: 0000, Tech: 0001, Missing
    • Cust: 0001, Tech: 0001, Missing
    • Cust: 0002, Tech: 0002, Not Missing
    • Cust: 0003, Tech: 0003, Not Missing
    • Cust: 0002, Tech: 0004, Not Missing
    • Cust: 0005, Tech: 0005, Missing
FrankE
  • 313
  • 1
  • 4
  • 14
0

You just want to confirm that the items in first list is exist in second list ?

If I am right then you had taken a long road. just take an item from one list and check whether it is exist on second list or not

example :

secondList.Exists(first[0]);

it will return bool.

Red
  • 39
  • 2
  • As far as I know, your suggestion only works on objects, and not on object properties. It may not be obvious in this example, but I also need to compare on property values, so objects that are only partly different. – Willem Apr 25 '12 at 12:16
  • Yes you are right BTW Cust: 0002, Tech: 0004, Not Missing <= here is a problem right ? – Red Apr 25 '12 at 14:12