Reputation:
I'm facing with the following problem:
I have two Lists which I would like to compare to find out if ListA contains all ListB's Items.
I also want to count them, so that I get how much elements are missing (if any).
Is there a simple and easy way to achieve this?
public static bool ContainsAllItems<T>(IEnumerable<T> a, IEnumerable<T> b)
{
return !b.Except(a).Any();
}
Expected
public static int ContainsAllItems<T>(IEnumerable<T> a, IEnumerable<T> b)
{
//Count how much Elements are missing or if there are some missing
return count(!b.Except(a).Any());
}
Upvotes: 0
Views: 256
Reputation: 16079
You can use Linq Count()
method to get count of elements which are present in List b
but missing in List a
Returns the number of elements in a sequence.
var count = b.Except(a).Count();
Your code will look like,
//Get count of elements which are present in List b, but missing in List a
public static int ContainsAllItems<T>(IEnumerable<T> a, IEnumerable<T> b)
{
return b.Except(a).Count();
}
Upvotes: 2