I know LINQ has a SequenceEquals method. This method makes sure each item value in each collection matches, in the same order.
What I’m looking for is a more “Equivalent” type of functionality. Just that both sequences contain the same items, not necessarily in the same order.
For example, nUnit has CollectionAssert.AreEqual() and CollectionAssert.AreEquivalent() that do what I’m explaining.
I know that I can do this either by:
- Ordering the lists ahead of time and using
SequenceEquals - Using
Intersect, then seeing if the intersection is equal to the original sequence.
Example:
var source = new[] {5, 6, 7};
source.Intersect(new[] {5, 7, 6}).Count() == source.Length;
I would create an extension method that does the intersect and then compares the counts.