116

I have the following method:

namespace ListHelper
{
    public class ListHelper<T>
    {
        public static bool ContainsAllItems(List<T> a, List<T> b)
        {
            return b.TrueForAll(delegate(T t)
            {
                return a.Contains(t);
            });
        }
    }
}

The purpose of which is to determine if a List contains all the elements of another list. It would appear to me that something like this would be built into .NET already, is that the case and am I duplicating functionality?

Edit: My apologies for not stating up front that I'm using this code on Mono version 2.4.2.

CompanyDroneFromSector7G
  • 4,291
  • 13
  • 54
  • 97
Matt Haley
  • 4,304
  • 4
  • 25
  • 17

5 Answers5

205

If you're using .NET 3.5, it's easy:

public class ListHelper<T>
{
    public static bool ContainsAllItems(List<T> a, List<T> b)
    {
        return !b.Except(a).Any();
    }
}

This checks whether there are any elements in b which aren't in a - and then inverts the result.

Note that it would be slightly more conventional to make the method generic rather than the class, and there's no reason to require List<T> instead of IEnumerable<T> - so this would probably be preferred:

public static class LinqExtras // Or whatever
{
    public static bool ContainsAllItems<T>(this IEnumerable<T> a, IEnumerable<T> b)
    {
        return !b.Except(a).Any();
    }
}
RMalke
  • 4,048
  • 29
  • 42
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
48

Included in .NET 4: Enumerable.All

public static bool ContainsAll<T>(IEnumerable<T> source, IEnumerable<T> values)
{
    return values.All(value => source.Contains(value));
}
orad
  • 15,272
  • 23
  • 77
  • 113
Thomas
  • 24,234
  • 6
  • 81
  • 125
36

Just for fun, @JonSkeet's answer as an extension method:

/// <summary>
/// Does a list contain all values of another list?
/// </summary>
/// <remarks>Needs .NET 3.5 or greater.  Source:  https://stackoverflow.com/a/1520664/1037948 </remarks>
/// <typeparam name="T">list value type</typeparam>
/// <param name="containingList">the larger list we're checking in</param>
/// <param name="lookupList">the list to look for in the containing list</param>
/// <returns>true if it has everything</returns>
public static bool ContainsAll<T>(this IEnumerable<T> containingList, IEnumerable<T> lookupList) {
    return ! lookupList.Except(containingList).Any();
}
Community
  • 1
  • 1
drzaus
  • 24,171
  • 16
  • 142
  • 201
  • 2
    similarly: Contains Any = `public static bool ContainsAny(this IEnumerable haystack, IEnumerable needle) { return haystack.Intersect(needle).Count() > 0; }`. I tried some quick performance comparisons to `haystack.Count() - 1 >= haystack.Except(needle).Count();` and `Intersect` seemed to do better most of the time. – drzaus May 23 '13 at 16:21
  • 4
    sheesh...use `Any()` not `Count() > 0`: `public static bool ContainsAny(this IEnumerable haystack, IEnumerable needle) { return haystack.Intersect(needle).Any(); }` – drzaus Jun 04 '13 at 14:34
3

I know a way using LinQ methods. It's a bit weird to read, but works pretty well

var motherList = new List<string> { "Hello", "World", "User };
var sonList = new List<string> { "Hello", "User" };

You want to check if sonList is totally in motherList

To do so:

sonList.All(str => moterList.Any(word => word == str));

// Reading literally, would be like "For each of all items 
// in sonList, test if it's in motherList

Please check it on and see if works there too. Hope it helps ;-)

  • This works. But please correct the typo in this line: `bool result = sonList.All(str => motherList.Any(word => word == str));` – Shubhjot Aug 04 '23 at 18:07
-1

You could also use another way. Override equals and use this

public bool ContainsAll(List<T> a,List<T> check)
{
   list l = new List<T>(check);
   foreach(T _t in a)
   {
      if(check.Contains(t))
      {
         check.Remove(t);
         if(check.Count == 0)
         {
            return true;
         }
      }
      return false;
   }
}
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
user3210251
  • 161
  • 1
  • 2
  • 9