I have two lists populated from different sources, what is the best way to check if both list contains the same items? Order is not important
List<Tuple<string, string, string>> list1;
List<Tuple<string, string, string>> list2;
I have two lists populated from different sources, what is the best way to check if both list contains the same items? Order is not important
List<Tuple<string, string, string>> list1;
List<Tuple<string, string, string>> list2;
You can use !Except.Any
:
bool same = list1.Count == list2.Count && !list1.Except(list2).Any();
Explanation:
Count
, otherwise you know they don't contain the sameExcept
and Any
if there are remeaining tuples if you "remove" list2
from list1
. If there are Any
(at least one) you know they don't contain the sameWorks because tuples override GetHashCode
(like anonymous types too) and string
too.