1

I have to compare two lists of same class and store the difference in a string.

public class SomeCustomClass {
      public int MaterialId { get; set; }
      public string CwNumber { get; set; }
      public string MaterialName { get; set; }
      public List<MyClass1> ListMyClass { get; set; }

      public SomeCustomClass() {
        ListMyClass = new List<MyClass1>();
      }      
   }

 static void Main() {
    SomeCustomClass a = new SomeCustomClass();
    SomeCustomClass b = new SomeCustomClass();
    a.CwNumber = "A";
    a.MaterialId = 1;
    a.MaterialName = "Material1";
    a.ListMyClass.Add( new MyClass1 { id = 11,Name="John" });
    a.ListMyClass.Add(new MyClass1 { id = 12, Name = "Naren" });       

    b.CwNumber = "A";
    b.MaterialId = 2;
    b.MaterialName = "Material2";
    b.ListMyClass.Add(new MyClass1 { id = 11, Name = "Tamsan" });
    b.ListMyClass.Add(new MyClass1 { id = 12, Name = "Naren" });
    b.ListMyClass.Add(new MyClass1 { id = 13, Name = "sanjy" });
}

Now I have to compare between a.ListMyClass and b.ListMyClass and store the difference as

   id = "11",
   Name="John":id = 11,
   Name = "Tamsan" | id = "",
   Name="":id = 13,
   Name = "sanjy"

The difference out put will contain -

   i. Name is different for an ID.
   ii. An object exists in first list but not present in 2nd list.( in that case out put string will be like id="someid",Name="somename":id="",Name="").     
   iii. An object present in 2nd list is not present in 1st list.  (in that case out put string will be like id="",Name="":id="someid",Name="somename").
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Anadi
  • 27
  • 2
  • 2
    What have u tried so far? – Mohit S Nov 26 '15 at 08:16
  • Please clarify desired answer, do you really want the empty object where the two lists contain similar objects? – Henrik Nov 26 '15 at 08:21
  • Can you please show what the difference as the equivalent of writing it in c#? In other words, are you asking for a single `string`, an enumerable of `string`, enumerable of anonymous object, etc? Please show the equivalent c#. – Enigmativity Nov 26 '15 at 08:21
  • 1
    Please be more specific on the desired result. Do you mean each object that does not occur in both lists, where the objects from the first list precede the objects from the second list? Also, do you expect to use the implemementation of `Equals` of `MyClass1` to define the actual comparison? – Codor Nov 26 '15 at 08:22
  • `public override bool Equals(object obj)` solve your problem. – Ankush Madankar Nov 26 '15 at 08:26
  • 1. object that does not occur in both lists 2. Object that occurs either of the list. – Anadi Nov 26 '15 at 08:28
  • Then please edit and fix question, sanjy only appears in one list, but is not represented before the pipe – Henrik Nov 26 '15 at 08:33
  • @AnkushMadankar - You can't just override `Equals`. It doesn't work. – Enigmativity Nov 26 '15 at 08:35
  • @Anadi: As Mohit already said, what have you tried? What exactly are you having trouble with? – Pieter Witvoet Nov 26 '15 at 08:36

1 Answers1

0

This will solve it, but it's not pretty

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class MyClass1
    {
        public int id;
        public string Name;

        public override string ToString()
        {
            return "id = \"" + id + "\", Name=\"" + Name + "\"";
        }
    }

    public class SomeCustomClass
    {
        public int MaterialId { get; set; }
        public string CwNumber { get; set; }
        public string MaterialName { get; set; }
        public List<MyClass1> ListMyClass { get; set; }

        public SomeCustomClass()
        {
            ListMyClass = new List<MyClass1>();
        }

        public static string operator -(SomeCustomClass b, SomeCustomClass c)
        {
            string commonIDsDifferentNames = "";
            string uniqueIDs = "";
            List<int> usedInC = new List<int>();

            int cCount = 0;
            foreach (MyClass1 element in b.ListMyClass)
            {
                cCount = 0;
                bool found = false;
                foreach (MyClass1 element2 in c.ListMyClass)
                {
                    if (element.id == element2.id)
                    {
                        found = true;
                        usedInC.Add(cCount);

                        if(element.Name == element2.Name)
                            break;

                        commonIDsDifferentNames += element + " : " + element2;
                    }
                    cCount++;
                }

                if(!found)
                {
                    uniqueIDs += element + " : id=\"\", Name=\"\"";
                }

            }

            cCount = -1;
            foreach (MyClass1 element2 in c.ListMyClass)
            {
                cCount++;
                if (usedInC.Contains(cCount))
                    continue;

                uniqueIDs += "id=\"\", Name=\"\" : " + element2;
            }

            return commonIDsDifferentNames + " | " + uniqueIDs;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            SomeCustomClass a = new SomeCustomClass();
            SomeCustomClass b = new SomeCustomClass();
            a.CwNumber = "A";
            a.MaterialId = 1;
            a.MaterialName = "Material1";
            a.ListMyClass.Add(new MyClass1 { id = 11, Name = "John" });
            a.ListMyClass.Add(new MyClass1 { id = 12, Name = "Naren" });

            b.CwNumber = "A";
            b.MaterialId = 2;
            b.MaterialName = "Material2";
            b.ListMyClass.Add(new MyClass1 { id = 11, Name = "Tamsan" });
            b.ListMyClass.Add(new MyClass1 { id = 12, Name = "Naren" });
            b.ListMyClass.Add(new MyClass1 { id = 13, Name = "sanjy" });

            Console.WriteLine(a - b);
            Console.ReadKey();
        }
    }
}
Henrik
  • 2,180
  • 16
  • 29
  • I'm aware that Except would produce a list of unique elements given an overloaded equals. However then you would have to analyze that to determine partially similar objects, or use a where(lambda expression) to get the partials, and a where(lambda expression) to get the uniques – Henrik Nov 26 '15 at 09:10