user13180906
user13180906

Reputation:

Count all Fields in Except

I'm facing with the following problem:

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

Answers (1)

Prasad Telkikar
Prasad Telkikar

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

Related Questions