Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- private static bool CheckLists(List<Product> list1, List<Product> list2)
- {
- var resultCount = list2.Count(x => !list1.Any(k => k.SupplierId == x.SupplierId));
- return (resultCount == 0);
- }
- if (list1.Select(p => p.SupplierId).Except(list2.Select(p => p.SupplierId)).Any())
- list1.All(x => list2.Any(y => x.SupplierId == y.SupplierId));
- private static bool CheckLists(List<Product> list1, List<Product> list2) => list2.All(l2p => list1.Any(l1p => l1p.SupplierId == l2p.SupplierId));
- 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);
- }
- var ans = list1.ContainsAll(list2, p => p.SupplierId);
Add Comment
Please, Sign In to add comment