Guest User

Untitled

a guest
Nov 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. var list1 = new List<Product>
  2. {
  3. new Product{SupplierId = 1,ProductName = "Name1"},
  4. new Product{SupplierId = 2,ProductName = "Name2"},
  5. new Product{SupplierId = 3,ProductName = "Name3"},
  6. new Product{SupplierId = 4,ProductName = "Name4"}
  7. };
  8. var list2 = new List<Product>
  9. {
  10. new Product {SupplierId = 1,ProductName = "Name5"},
  11. new Product {SupplierId = 4,ProductName = "Name6"}
  12. };
  13.  
  14. private static bool CheckLists(List<Product> list1, List<Product> list2)
  15. {
  16. foreach (var product2 in list2)
  17. {
  18. bool result = false;
  19. foreach (var product in list1)
  20. {
  21. if (product.SupplierId == product2.SupplierId)
  22. {
  23. result = true;
  24. break;
  25. }
  26. }
  27. if (!result)
  28. {
  29. return false;
  30. }
  31. }
  32. return true;
  33. }
  34.  
  35. private static bool CheckLists(List<Product> list1, List<Product> list2)
  36. {
  37. var resultCount = list2.Count(x => !list1.Any(k => k.SupplierId == x.SupplierId));
  38. return (resultCount == 0);
  39. }
  40.  
  41. if (list1.Select(p => p.SupplierId).Except(list2.Select(p => p.SupplierId)).Any())
  42.  
  43. list1.All(x => list2.Any(y => x.SupplierId == y.SupplierId));
  44.  
  45. private static bool CheckLists(List<Product> list1, List<Product> list2) => list2.All(l2p => list1.Any(l1p => l1p.SupplierId == l2p.SupplierId));
  46.  
  47. public static class Ext {
  48. static public bool ContainsAll<T, TKey>(this List<T> containingList, List<T> containee, Func<T, TKey> key) {
  49. var HSContainingList = new HashSet<TKey>(containingList.Select(key));
  50. return containee.All(l2p => HSContainingList.Contains(key(l2p)));
  51. }
  52. static public bool ContainsAll<T>(this List<T> containingList, List<T> containee) => containingList.ContainsAll(containee, item => item);
  53. }
  54.  
  55. var ans = list1.ContainsAll(list2, p => p.SupplierId);
Add Comment
Please, Sign In to add comment