If you are using .NET 3.5, this is easy:
public class ListHelper<T> { public static bool ContainsAllItems(List<T> a, List<T> b) { return !b.Except(a).Any(); } }
This checks to see if there are any elements in b that are not in a and then inverts the result.
Note that it would be a little more familiar to make the method universal rather than a class, and there is no reason to require a List<T> instead of an IEnumerable<T> - so this would probably be preferable:
public static class LinqExtras // Or whatever { public static bool ContainsAllItems<T>(this IEnumerable<T> a, IEnumerable<T> b) { return !b.Except(a).Any(); } }
Jon Skeet Oct 05 '09 at 15:06 2009-10-05 15:06
source share