Check if one IEnumerable contains all elements of another IEnumerable

There is no "fast way" to do this unless you track and maintain some state that determines whether all values in one collection are contained in another. If you only have IEnumerable<T> to work against, I would use Intersect.

var allOfList1IsInList2 = list1.Intersect(list2).Count() == list1.Count();

The performance of this should be very reasonable, since Intersect() will enumerate over each list just once. Also, the second call to Count() will be optimal if the underlying type is an ICollection<T> rather than just an IEnumerable<T>.


C# 3.5+

Using Enumerable.All<TSource> to determine if all List2 items are contained in List1:

bool hasAll = list2Uris.All(itm2 => list1Uris.Contains(itm2));

This will also work when list1 contains even more than all the items of list2.


You could also use Except to remove from the first list all values that exist in the second list, and then check if all values have been removed:

var allOfList1IsInList2 = !list1.Except(list2).Any();

This method had the advantage of not requiring two calls to Count().