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 @@ - - - - + + + +