2

I have two List, this complex object has 4 public properties of reference type. How can i compare one list to another to find if those lists are equal in size and by values.

I have implemented Equals in ComplexObject in order to help with equality checks

public Type1 Type1 { get; set; } public IEnumerable<Type2> Type2s{ get; set; } public Type3 Type3{ get; set; } public Type4 Type4 { get; set; } public bool Equals(ComplexObject complexObject) { int type2sCount = Type2s.Count(); return Type1 .Equals(complexObject.Type1) && Type3.Equals(complexObject.Type3) && Type4.Equals(complexObject.Type4) && Type2s.Intersect(complexObject.Type2s).Count() == type2sCount; } 

I need also to print out items that do no fit or have no pair in second list

Thanks

4 Answers 4

1

The simplest approach for the lists would be SequenceEqual, but you need to be careful about nulls:

public bool Equals(ComplexObject complexObject) { bool eq = Equals(Type1, complexObject.Type1) && Equals(Type3, complexObject.Type3) && Equals(Type4, complexObject.Type4); if (eq) { if(Type2s == null) { if(complexObject.Type2s != null) eq = false; } else { eq = complexObject.Type2s == null ? false : Type2s.SequenceEqual(complexObject.Type2s); } } return eq; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Should i sort list prior using this approach ?
@eugeneK that depends... do you consider them as "sets" or "sequences"? I would propose that the sequence {1,2,3} is not equal to the sequence {1,3,2}, however if we consider them as sets: they are. Either way, I wouldn't do any sorting. If we were treating them as sets, I would probably use a HashSet<T> buffer or something. Of course, then it gets tricky if you consider {1,1,2,3} to be different to {1,2,3}, because if we are treating them as sets: they are the same.
Well these are sets, so i can use your Equals suggestion along with @Charlie - BlokeTech answer. Unless there is a better solution. I couldn't get how HasSet<T> would help me though.
1

If you sort both lists first, you can use the SequencyEqual extension method to check that both sequences are equal. This method is part of System.Linq.

Here's an example:

 List<ComplexObject> list1 = new List<ComplexObject>(); List<ComplexObject> list2 = new List<ComplexObject>(); IOrderedEnumerable<ComplexObject> list1Sorted = list1.OrderBy(item => item.SomeProperty); IOrderedEnumerable<ComplexObject> list2Sorted = list2.OrderBy(item => item.SomeProperty); bool areEqual = list1Sorted.SequenceEqual(list2Sorted); 

1 Comment

This also depends on whether you consider the sequences {1,2,3} and {1,3,2} to be "equal"... sometimes you do, sometimes you don't.
0

Check out the IEqualityComparer(T). You implement the interface members and if you are using LINQ, it will work, you should be able to AND the lists. http://msdn.microsoft.com/en-us/library/ms132151.aspx

2 Comments

Is there any generic Comparer so i can use it for other kind of objects ?
@eugeneK yes: EqualityComparer<T>.Default
0

You could make use of SequenceEqual(List1, List2)

 bool equal = Type2s.SequenceEqual(complexObject.Type2s); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.