I have 2 string lists List<string>
listA
listB
I need to know if all of listB
is in listA
. listA
is the bigger list and we need to make sure all of listB
is in it.
Is that a Union?
No, it's not a union. The simplest approach is to check whether there's anything in listB
which isn't in listA
:
var result = !listB.Except(listA).Any();
In other words, if you took everything from listA
away from listB
, would there be anything left or not?
Or of course you could do it more explicitly:
// Optional: makes the Contains check quicker.
var setA = new HashSet<string>(setA);
var result = listB.All(setA.Contains);
See more on this question at Stackoverflow