From 10c0b16c403495e13f5532be7a767a47cb249d3b Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 18 Oct 2026 14:19:46 +0200 Subject: [PATCH] Lookup in C#: port to .NET 10, add GroupBy snapshot example and tests Move both projects to net10.0, update the test packages (xunit 2.9.3, xunit.runner.visualstudio 4.0.1, Microsoft.NET.Test.Sdk 18.11.0, coverlet.collector 10.1.0), add CountValuesAfterSourceChanges to compare ToLookup with GroupBy, and add tests for a missing key and for a source that changes after the call. --- .../LookupInCSharp/LookupInCSharp.csproj | 2 +- .../LookupInCSharp/LookupOperations.cs | 15 +++++++++++++- .../Tests/LookupOperationsTest.cs | 20 ++++++++++++++++++- csharp-linq/LookupInCSharp/Tests/Tests.csproj | 10 +++++----- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/csharp-linq/LookupInCSharp/LookupInCSharp/LookupInCSharp.csproj b/csharp-linq/LookupInCSharp/LookupInCSharp/LookupInCSharp.csproj index 405f6f99f7..eb9c09aae9 100644 --- a/csharp-linq/LookupInCSharp/LookupInCSharp/LookupInCSharp.csproj +++ b/csharp-linq/LookupInCSharp/LookupInCSharp/LookupInCSharp.csproj @@ -2,7 +2,7 @@ Library - net8.0 + net10.0 enable enable diff --git a/csharp-linq/LookupInCSharp/LookupInCSharp/LookupOperations.cs b/csharp-linq/LookupInCSharp/LookupInCSharp/LookupOperations.cs index b9917d20cd..4fddb78b14 100644 --- a/csharp-linq/LookupInCSharp/LookupInCSharp/LookupOperations.cs +++ b/csharp-linq/LookupInCSharp/LookupInCSharp/LookupOperations.cs @@ -30,4 +30,17 @@ public static bool SearchForKeyInLookup(string key) public static int GetLookupCount() => _lookup.Count; -} \ No newline at end of file + + public static (int FromGroupBy, int FromLookup) CountValuesAfterSourceChanges( + string key, Student newStudent) + { + List students = [.. _students]; + + var grouped = students.GroupBy(s => s.Name, s => s.Course); + var lookup = students.ToLookup(s => s.Name, s => s.Course); + + students.Add(newStudent); + + return (grouped.Single(g => g.Key == key).Count(), lookup[key].Count()); + } +} diff --git a/csharp-linq/LookupInCSharp/Tests/LookupOperationsTest.cs b/csharp-linq/LookupInCSharp/Tests/LookupOperationsTest.cs index 89fdf218ea..b8dcc8ae83 100644 --- a/csharp-linq/LookupInCSharp/Tests/LookupOperationsTest.cs +++ b/csharp-linq/LookupInCSharp/Tests/LookupOperationsTest.cs @@ -75,4 +75,22 @@ public void WhenGetLookupCountIsCalled_ThenReturnsTheLookupCount() Assert.Equal(3, count); } -} \ No newline at end of file + + [Fact] + public void GivenKeyThatDoesNotExist_WhenRetrieveValuesOfAKeyFromLookupIsCalled_ThenReturnsEmpty() + { + var values = LookupOperations.RetrieveValuesOfAKeyFromLookup("Michael Sorla"); + + Assert.Empty(values); + } + + [Fact] + public void GivenSourceChangesAfterTheCall_WhenCountValuesAfterSourceChangesIsCalled_ThenOnlyGroupBySeesTheChange() + { + var (fromGroupBy, fromLookup) = LookupOperations.CountValuesAfterSourceChanges( + "Kate Green", new Student("Kate Green", "Taxation")); + + Assert.Equal(2, fromGroupBy); + Assert.Equal(1, fromLookup); + } +} diff --git a/csharp-linq/LookupInCSharp/Tests/Tests.csproj b/csharp-linq/LookupInCSharp/Tests/Tests.csproj index 5c9a7737e2..2b987baa22 100644 --- a/csharp-linq/LookupInCSharp/Tests/Tests.csproj +++ b/csharp-linq/LookupInCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net8.0 + net10.0 enable enable @@ -10,10 +10,10 @@ - - - - + + + +