Determine if a sequence contains all elements of another sequence using Linq [duplicate]
Count? How about Not Any?
bool contained = !subset.Except(superset).Any();
So, my other answer was pretty easy to use. But it's an O(n*m) solution.
Here's a slightly less friendly O(n+m) solution. This should be used if superset is HUGE. It avoids repeatedly enumerating superset.
HashSet<int> hashSet = new HashSet<int>(superset);
bool contained = subset.All(i => hashSet.Contains(i));
I have an extension method that uses the existing Contains()-method. I find it more intuitive than using Instersect() or Except().
public static bool ContainsAll<T>(this IEnumerable<T> source, IEnumerable<T> values)
{
return values.All(value => source.Contains(value));
}
You could use Except and the resulting count should be 0.
Read up on MSDN for details of the parameters.
Example:
subset.Except(superset).Count() == 0