Check if a list contains all of another lists items when comparing on one property

Viewed 24373

I am learning Linq and I have two object lists. I want to compare one of these lists against the other to see if all of one of the properties of the objects within it can be matched to those in the other list. So, I provide code for that but I want to change it to Linq expressions.

var list1 = new List<Product>
{
    new Product{SupplierId = 1,ProductName = "Name1"},
    new Product{SupplierId = 2,ProductName = "Name2"},
    new Product{SupplierId = 3,ProductName = "Name3"},
    new Product{SupplierId = 4,ProductName = "Name4"}
};

var list2 = new List<Product>
{
    new Product {SupplierId = 1,ProductName = "Name5"},
    new Product {SupplierId = 4,ProductName = "Name6"}
};

private static bool CheckLists(List<Product> list1, List<Product> list2)
{
    foreach (var product2 in list2)
    {
        bool result = false;
        foreach (var product in list1)
        {
            if (product.SupplierId == product2.SupplierId)
            {
                result = true;
                break;
            }
        }
        if (!result)
        {
            return false;
        }
    }
    return true;
}

How can I do it using LINQ?

3 Answers
bool existsCheck = list1.All(x => list2.Any(y => x.SupplierId == y.SupplierId));

would tell you if all of list1's items are in list2.

You want to check whether there are any IDs in list1 that aren't in list2:

if (list1.Select(p => p.SupplierId).Except(list2.Select(p => p.SupplierId)).Any())

To see that list1 contains all of list2, check if Any of list1 matches All of list2:

private static bool CheckLists(List<Product> list1, List<Product> list2) => list2.All(l2p => list1.Any(l1p => l1p.SupplierId == l2p.SupplierId));

However, I would probably write an generic extension method:

public static class Ext {
    static public bool ContainsAll<T, TKey>(this List<T> containingList, List<T> containee, Func<T, TKey> key) {
        var HSContainingList = new HashSet<TKey>(containingList.Select(key));
        return containee.All(l2p => HSContainingList.Contains(key(l2p)));
    }
    static public bool ContainsAll<T>(this List<T> containingList, List<T> containee) => containingList.ContainsAll(containee, item => item);
}

Then you can call it like:

var ans = list1.ContainsAll(list2, p => p.SupplierId);
Related