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();
bool isequal = duplicate4.sequenceequal(duplicate3)
&& duplicate3.sequenceequal(duplicate2)
&& duplicate2.sequenceequal(duplicate1)
&& duplicate1.sequenceequal(original);
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())));
.