Does .NET have a way to check if List a contains all items in List b?
C#.NetListGenericsMonoC# Problem Overview
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.
C# Solutions
Solution 1 - C#
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();
}
}
Solution 2 - C#
Included in .NET 4: Enumerable.All
public static bool ContainsAll<T>(IEnumerable<T> source, IEnumerable<T> values)
{
return values.All(value => source.Contains(value));
}
Solution 3 - C#
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();
}
Solution 4 - C#
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 ;-)
Solution 5 - C#
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;
}
}