Reputation: 686
I have two IEnumerable
objects and I want to validate if one of them contains all the elements of the other.
I'm using obj1.Intersect(obj2).Any()
but the intersection is not working as I'd expect. It returns true even just one of the elements in obj2
exists in obj1
.
Is there any way to verify if all the elements of obj2
exist in obj1
?
Upvotes: 3
Views: 1253
Reputation: 43743
There is no single LINQ method which does what you need without at least specifying a lambda. There are, however, multiple ways to do it with LINQ. Here are a few options (to test if obj2
is a subset of obj1
):
obj1.Intersect(obj2).Count() == obj2.Count()
or
obj2.All(x=>obj1.Contains(x))
or
obj2.Except(obj1).Any()
Upvotes: 7