0

So far I have

  @if (!(Model.CurrentVersion.LRC.List == Model.PrevVersion.LRC.List))

I want to see if the list from the previous version matches the current version however this only returns true (with the !). Both of the lists are empty but it's not returning false.

Is there a better way of seeing if lists match? And why is it always returning true?

Thanks!

Pikapops
  • 631
  • 2
  • 8
  • 22
  • 1
    It is saying that because you are checking if the lists are the same objects instead of checking if they have the same content. If they are both empty they are still not the same objects – StijnvanGaal Apr 28 '17 at 09:37
  • You may have a look at these solutions too (these may give you an idea on how to solve your problem): http://stackoverflow.com/questions/16789336/c-sharp-check-if-a-list-is-a-part-of-another-list – TechManiac Apr 28 '17 at 09:40
  • Try use `SequenceEqual` or `All` with `Count` check, e.g. `Enumerable.SequenceEqual(Model.CurrentVersion.LRC.List, Model.PrevVersion.LRC.List);`. – Tetsuya Yamamoto Apr 28 '17 at 09:41
  • Possible duplicate of [Compare two List objects for equality, ignoring order](http://stackoverflow.com/questions/3669970/compare-two-listt-objects-for-equality-ignoring-order) – Tetsuya Yamamoto Apr 28 '17 at 09:48

1 Answers1

1

You need to check if the content of the lists are equal. There are several ways of doing so. If the order of items is important then try SequenceEqual

@if(!Model.CurrentVersion.LRC.List.SequenceEqual(Model.PrevVersion.LRC.List))

If you don't care about the order of the items in the lists you could use

!ints1.All(ints5.Contains)

Now you still have the problem that you are comparing if the items in the list are the same objects. You might want to check if theses items equal in content. For that you need to implement an IEqualityComparer<T>. In the SequenceEqual page there is a great example to implement this case.

StijnvanGaal
  • 441
  • 3
  • 17