From cde09ac8e9da3299482cc537a03edd8c72b25d13 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Mon, 24 Aug 2026 00:21:24 +0200 Subject: [PATCH] HowToCompareTwoListsThroughOneProperty: retarget net10.0, add ExceptByMethod Retarget all three projects in the sample from net8.0 to net10.0 and add an ExceptByMethod alongside the five existing comparison methods, so the article can show how to find the difference between two lists and not only the overlap. - Benchmark, tests and app csproj: net8.0 -> net10.0 - ListCompareMethods.ExceptByMethod: projects the order keys and excludes by them with ExceptBy, returning the customers with no orders - Program.cs: prints the new method's result alongside the other five - UnitTests: three tests for ExceptByMethod (partial overlap, full overlap, empty order list) Build clean, 23/23 tests pass on net10.0. --- .../Benchmark/Benchmark.csproj | 2 +- .../HowToCompareTwoLists.Tests.csproj | 2 +- .../HowToCompareTwoLists.Tests/UnitTests.cs | 33 ++++++++++++++++++- ...ToCompareTwoListsThroughOneProperty.csproj | 2 +- .../Methods/ListCompareMethods.cs | 7 ++++ .../Program.cs | 5 ++- 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/collections-lists/HowToCompareTwoListsThroughOneProperty/Benchmark/Benchmark.csproj b/collections-lists/HowToCompareTwoListsThroughOneProperty/Benchmark/Benchmark.csproj index 3d8903a8b..6ddf9fa0e 100644 --- a/collections-lists/HowToCompareTwoListsThroughOneProperty/Benchmark/Benchmark.csproj +++ b/collections-lists/HowToCompareTwoListsThroughOneProperty/Benchmark/Benchmark.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net10.0 enable enable diff --git a/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoLists.Tests/HowToCompareTwoLists.Tests.csproj b/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoLists.Tests/HowToCompareTwoLists.Tests.csproj index b86b66422..95317a21b 100644 --- a/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoLists.Tests/HowToCompareTwoLists.Tests.csproj +++ b/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoLists.Tests/HowToCompareTwoLists.Tests.csproj @@ -1,7 +1,7 @@ - net8.0 + net10.0 enable enable diff --git a/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoLists.Tests/UnitTests.cs b/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoLists.Tests/UnitTests.cs index 05e7f1bb0..b55fb88f0 100644 --- a/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoLists.Tests/UnitTests.cs +++ b/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoLists.Tests/UnitTests.cs @@ -250,4 +250,35 @@ public void GivenJoinListMethod_WhenNoCustomers_ThenReturnEmptyList() Assert.That(result, Is.Not.Null); Assert.That(result, Has.Count.EqualTo(0)); } -} \ No newline at end of file + + [Test] + public void GivenExceptByMethod_WhenThreeCustomersWithTwoOrders_ThenReturnCustomerWithoutOrders() + { + var result = ListCompareMethods.ExceptByMethod(_threeCustomers, _twoOrders); + + Assert.That(result, Is.Not.Null); + Assert.That(result, Has.Count.EqualTo(1)); + + CollectionAssert.Contains(result, _threeCustomers[2]); + CollectionAssert.DoesNotContain(result, _threeCustomers[0]); + CollectionAssert.DoesNotContain(result, _threeCustomers[1]); + } + + [Test] + public void GivenExceptByMethod_WhenThreeCustomersWithThreeOrders_ThenReturnEmptyList() + { + var result = ListCompareMethods.ExceptByMethod(_threeCustomers, _threeOrders); + + Assert.That(result, Is.Not.Null); + Assert.That(result, Has.Count.EqualTo(0)); + } + + [Test] + public void GivenExceptByMethod_WhenNoOrders_ThenReturnAllCustomers() + { + var result = ListCompareMethods.ExceptByMethod(_threeCustomers, []); + + Assert.That(result, Is.Not.Null); + Assert.That(result, Has.Count.EqualTo(3)); + } +} diff --git a/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty.csproj b/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty.csproj index 2150e3797..ed9781c22 100644 --- a/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty.csproj +++ b/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net10.0 enable enable diff --git a/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/Methods/ListCompareMethods.cs b/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/Methods/ListCompareMethods.cs index 1a157d2fb..d318a62fd 100644 --- a/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/Methods/ListCompareMethods.cs +++ b/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/Methods/ListCompareMethods.cs @@ -53,4 +53,11 @@ public static List JoinListMethod(List customerList, List customer ).Distinct().ToList(); } + + public static List ExceptByMethod(List customerList, List orderList) + { + var orderedIds = orderList.Select(o => o.CustomerId); + + return customerList.ExceptBy(orderedIds, c => c.Id).ToList(); + } } diff --git a/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/Program.cs b/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/Program.cs index c190511f3..7e91890ee 100644 --- a/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/Program.cs +++ b/collections-lists/HowToCompareTwoListsThroughOneProperty/HowToCompareTwoListsThroughOneProperty/Program.cs @@ -28,4 +28,7 @@ Console.WriteLine(string.Join(',', customerWithOrdersD.Select(i => i.Firstname))); var customerWithOrdersE = ListCompareMethods.JoinListMethod(customers, orders); -Console.WriteLine(string.Join(',', customerWithOrdersE.Select(i => i.Firstname))); \ No newline at end of file +Console.WriteLine(string.Join(',', customerWithOrdersE.Select(i => i.Firstname))); + +var customersWithoutOrders = ListCompareMethods.ExceptByMethod(customers, orders); +Console.WriteLine(string.Join(',', customersWithoutOrders.Select(i => i.Firstname)));