How to check the contents of collections (> 2) are the same

I have a list. For good reasons, I duplicate the list many times and use it for different purposes. At some point, I need to check if the contents of all these collections match.

Ok, I know how to do this. But, being a fan of short-handed coding (linq ...), I would like to know if I can test this EFFECTIVELY with the shortest number of lines of code.

    List<string> original, duplicate1, duplicate2, duplicate3, duplicate4
                                       = new List<string();

        //...some code.....
        bool isequal = duplicate4.sequenceequal(duplicate3) 
         && duplicate3.sequenceequal(duplicate2)
         && duplicate2.sequenceequal(duplicate1) 
         && duplicate1.sequenceequal(original);//can we do it better than this 

UPDATE

Codeinchaos pointed out some of the senaria I was thinking about (duplicates and list order). Although sequenceequal will take care of duplicates, the order of the list can be a problem. Therefore, I change the code as follows. I need to copy the lists for this.

List<List<string>> copy = new List<List<int>> { duplicate1, duplicate2,  
                                                 duplicate3, duplicate4 }; 
bool iseqaul  = (original.All(x => (copy.All(y => y.Remove(x))))
                                         && copy.All(n => n.Count == 0)); 

UPDATE2

Eric- HashSet . .

List<HashSet<string>> copy2 =new List<HashSet<string>>{new HashSet<string>(duplicate1),
                                                       new HashSet<string>(duplicate2),
                                                       new HashSet<string> duplicate3),
                                                       new HashSet<string>(duplicate4)};
  HashSet<string> origninalhashset = new HashSet<string>(original);
  bool eq = copy2.All(x => origninalhashset.SetEquals(x));

Update3 Eric - SequenceEqual . Sequenceequal , sequenceequal. , , (nlogn).

UPDATE4 , .

var originallkup = original.ToLookup(i => i);    
var lookuplist = new List<ILookup<int, int>>
                                    {   duplicate4.ToLookup(i=>  i), 
                                        duplicate3.ToLookup(i=>  i), 
                                        duplicate2.ToLookup(i=>  i),
                                        duplicate1.ToLookup(i=>  i)
                                    };

bool isequal = (lookuplist.Sum(x => x.Count) == (originallkup.Count * 4)) &&       
   (originallkup.All(x => lookuplist.All(i => i[x.Key].Count() == x.Count())));

.

+5
3

. . - , .

:

? ?

:

. HashSet<T>, List<T>, , .

- , SetEquals, , - .

: , . - , SetEquals , .

+7

, , bash:

var allLists = new List<List<string>>() { original, duplicate1, duplicate2, duplicate3, duplicate4 };

bool allEqual = allLists.All(l => l.SequenceEqual(original));

, Any - .

bool allEqual = !allLists.Any(l => !l.SequenceEqual(original));

EDIT: , Any , . MSDN.

EDIT # 2: SequenceEquals. , SequenceEquals . , List<string>, . , , SequenceEquals .

+4

You can use reflection to create a common comparator and always use it. Check out this topic, you have some code that can help you: Compare two collections for equality regardless of the order of the elements in them

0
source

All Articles