-21
public class ClassA
{
    public enum1 Type { get;set;}
    public List<ClassB> lst1 {get;  set;}
}

public class ClassB
{
     public string var1 { get;set;}
     public int var2 { get;set;}
     public List<int> var3 { get;set;}          
     public List<int> var4 { get;set;}          
}

List<ClassA> lst1=Somefunction1();
List<ClassA> lst2=Somefunction2();

How can I check if lst1 and lst2 are equal considering order and also if I want to exclude one property var1 while comparing.

This is what I've got so far:

var firstNotSecond = lst1.Except(lst2).ToList();
var secondNotFirst = lst2.Except(lst1).ToList();

if ((firstNotSecond.Count > 0) || (secondNotFirst.Count > 0))
{
    // not equal result = false;
}

As you can see, both lst1 and lst2 are lists of ClassA items. Normal definitions of lists being equal is that they are equal if each item in the first list equals the corresponding item in the second list. With corresponding I mean the item with the same index.

However, I don't want normal equality comparison of objects of class A, I want to specify specific equality comparers

  • Two objects of ClassB are considered equal if the properties var2 / var3 / var 4 have equal value.
  • Two objects of ClassA are equal if property Type has the same value and property lst1 of both classA objects are Enumerable.SequenceEqual

I assume this has to be done by implementing IEquality for these classes, or maybe creating an EqualityComparerer and Use SequenceEqual for my lst1 and lst2

chetanya gehlot
  • 564
  • 4
  • 11
  • 2
    Which two lists? What is the contents of `var1` An integer value? A comma-separated list of integers? What has `ClassA` to do with your question? What have you tried? – Patrick Hofman Dec 21 '15 at 08:36
  • 1
    this is going to be a gone case... – Ian Dec 21 '15 at 08:37
  • See this: http://stackoverflow.com/questions/3669970/compare-two-listt-objects-for-equality-ignoring-order and some other posts... – Ian Dec 21 '15 at 08:43
  • i have tried this solution. got the error "At least one argument has to implement IComparable interface" – chetanya gehlot Dec 21 '15 at 08:46
  • Your post is a little better already. Still it is unclear what `var1` has to do with it. – Patrick Hofman Dec 21 '15 at 08:53
  • i want to exclude the value of var1 of class B while comparing the lst1 and lst2 – chetanya gehlot Dec 21 '15 at 08:55
  • 1
    @HaraldDutch please let the thinking be done by OP. Don't rewrite his post to reflect what you think the problem is. – Patrick Hofman Dec 21 '15 at 09:02
  • Normally I would do what you suggest. But apparently the asker had trouble defining what he meant. By trying to describe it precisely I thought I'd help him rephrase the question if my editing wasn't exactly what he wanted. In the help I read the following phrase: Be sure that you've read the close notice and any comments on the question so you can address any concerns raised there. Addressing the concerns often means editing the post, >>> which any user may do. <<< – Harald Coppoolse Dec 21 '15 at 09:11
  • 1
    Step 1 create a class that implements [IEqualityComparer](https://msdn.microsoft.com/library/ms132151.aspx) and does the exact equality comparison you want for this case. Step 2: `if (!lst1.SequenceEquals(lst2, instanceOfTheEqualityComparer)) { /* not (sequence) equal */ }` – Corak Dec 21 '15 at 09:46
  • @Corak your answer really works – chetanya gehlot Dec 21 '15 at 10:48

1 Answers1

4
    public class ClassBEqualityComparer : IEqualityComparer<ClassB>
        {
            public bool Equals(ClassB x, ClassB y)
            {

              if((x.var2.Equals(y.var2)) && (x.var3.SequenceEqual(y.var3)) && (x.var4.SequenceEqual(y.var4)))
              {
                return true;
              }
              else
              {
                return false;
              }       
            }
            public int GetHashCode(ClassB x)
            {

            }
        }



public class ClassAEqualityComparer : IEqualityComparer<ClassA>
{
    public bool Equals(ClassA x, ClassA y)
    {
      ClassBEqualityComparer ClassBEqC = new ClassBEqualityComparer();
      if((x.Type == y.Type) && (x.lst1.SequenceEqual(y.lst1,ClassBEqC)))
      {
        return true;
      }
      else
      {
        return false;
      }        

    }
    public int GetHashCode(ClassA x)
    {

    }
}   



    ClassAEqualityComparer ClassAEqC = new ClassAEqualityComparer();
    bool result = lst1.SequenceEqual(lst2, ClassAEqC);
chetanya gehlot
  • 564
  • 4
  • 11