diff --git a/csharp-linq/ToDictionaryMethodInCSharp/Tests/Tests.csproj b/csharp-linq/ToDictionaryMethodInCSharp/Tests/Tests.csproj index 5fcb979804..3d204e7f16 100644 --- a/csharp-linq/ToDictionaryMethodInCSharp/Tests/Tests.csproj +++ b/csharp-linq/ToDictionaryMethodInCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net8.0 + net10.0 enable enable @@ -10,13 +10,13 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/csharp-linq/ToDictionaryMethodInCSharp/Tests/ToDictionaryOperationsTest.cs b/csharp-linq/ToDictionaryMethodInCSharp/Tests/ToDictionaryOperationsTest.cs index e6c2717e93..c597ca1bd7 100644 --- a/csharp-linq/ToDictionaryMethodInCSharp/Tests/ToDictionaryOperationsTest.cs +++ b/csharp-linq/ToDictionaryMethodInCSharp/Tests/ToDictionaryOperationsTest.cs @@ -10,7 +10,8 @@ private static readonly Dictionary _expectedCountries { "Egypt", new("Egypt", "Cairo", "Africa") }, { "India", new("India", "New Delhi", "Asia") }, { "Chile", new("Chile", "Santiago", "South America") }, - { "Australia", new("Australia", "Canberra", "Oceania") } + { "Australia", new("Australia", "Canberra", "Oceania") }, + { "Kenya", new("Kenya", "Nairobi", "Africa") } }; private static readonly Dictionary _expectedCountriesandCapitals @@ -19,7 +20,8 @@ private static readonly Dictionary _expectedCountriesandCapitals { "Egypt", "Cairo" }, { "India", "New Delhi" }, { "Chile", "Santiago" }, - { "Australia", "Canberra" } + { "Australia", "Canberra" }, + { "Kenya", "Nairobi" } }; [Fact] @@ -37,4 +39,57 @@ public void WhenCallToDictionaryWithKeysAndValuesIsCalled_ThenReturnsDictionary( Assert.True(_expectedCountriesandCapitals.OrderBy(x => x.Key).SequenceEqual(result.OrderBy(x => x.Key))); } + + [Fact] + public void WhenCallToDictionaryOnKeyValuePairsIsCalled_ThenReturnsDictionary() + { + var result = ToDictionaryOperations.CallToDictionaryOnKeyValuePairs(); + + Assert.True(_expectedCountriesandCapitals.OrderBy(x => x.Key).SequenceEqual(result.OrderBy(x => x.Key))); + } + + [Fact] + public void WhenCallToDictionaryOnTuplesIsCalled_ThenReturnsDictionary() + { + var result = ToDictionaryOperations.CallToDictionaryOnTuples(); + + Assert.True(_expectedCountriesandCapitals.OrderBy(x => x.Key).SequenceEqual(result.OrderBy(x => x.Key))); + } + + [Fact] + public void WhenCallToDictionaryWithComparerIsCalled_ThenLookupIgnoresCase() + { + var result = ToDictionaryOperations.CallToDictionaryWithComparer(); + + Assert.Same(StringComparer.OrdinalIgnoreCase, result.Comparer); + Assert.Equal("Cairo", result["egypt"].Capital); + } + + [Fact] + public void WhenCallToDictionaryWithDuplicateKeysIsCalled_ThenThrowsArgumentException() + { + var exception = Assert.Throws( + () => ToDictionaryOperations.CallToDictionaryWithDuplicateKeys()); + + Assert.Equal("An item with the same key has already been added. Key: Africa", exception.Message); + } + + [Fact] + public void WhenCallToDictionaryKeepingFirstPerContinentIsCalled_ThenKeepsFirstCountry() + { + var result = ToDictionaryOperations.CallToDictionaryKeepingFirstPerContinent(); + + Assert.Equal(4, result.Count); + Assert.Equal("Egypt", result["Africa"].Name); + } + + [Fact] + public void WhenCallToLookupOnContinentsIsCalled_ThenGroupsCountriesPerContinent() + { + var result = ToDictionaryOperations.CallToLookupOnContinents(); + + Assert.Equal(4, result.Count); + Assert.Equal(2, result["Africa"].Count()); + Assert.Empty(result["Antarctica"]); + } } \ No newline at end of file diff --git a/csharp-linq/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp.csproj b/csharp-linq/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp.csproj index fa71b7ae6a..b760144708 100644 --- a/csharp-linq/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp.csproj +++ b/csharp-linq/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp.csproj @@ -1,7 +1,7 @@  - net8.0 + net10.0 enable enable diff --git a/csharp-linq/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp/ToDictionaryOperations.cs b/csharp-linq/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp/ToDictionaryOperations.cs index f6eb173cbc..a2a4b86e8b 100644 --- a/csharp-linq/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp/ToDictionaryOperations.cs +++ b/csharp-linq/ToDictionaryMethodInCSharp/ToDictionaryMethodInCSharp/ToDictionaryOperations.cs @@ -7,7 +7,8 @@ private static readonly List _countries new("Egypt", "Cairo", "Africa"), new("India", "New Delhi", "Asia"), new("Chile", "Santiago", "South America"), - new("Australia", "Canberra", "Oceania") + new("Australia", "Canberra", "Oceania"), + new("Kenya", "Nairobi", "Africa") ]; public static Dictionary CallToDictionaryWithKeysOnly() @@ -15,4 +16,29 @@ public static Dictionary CallToDictionaryWithKeysOnly() public static Dictionary CallToDictionaryWithKeysAndValues() => _countries.ToDictionary(c => c.Name, c => c.Capital); + + public static Dictionary CallToDictionaryOnKeyValuePairs() + => _countries + .Select(c => new KeyValuePair(c.Name, c.Capital)) + .ToDictionary(); + + public static Dictionary CallToDictionaryOnTuples() + => _countries + .Select(c => (c.Name, c.Capital)) + .ToDictionary(); + + public static Dictionary CallToDictionaryWithComparer() + => _countries.ToDictionary(c => c.Name, StringComparer.OrdinalIgnoreCase); + + // Throws: two countries share the continent "Africa" + public static Dictionary CallToDictionaryWithDuplicateKeys() + => _countries.ToDictionary(c => c.Continent); + + public static Dictionary CallToDictionaryKeepingFirstPerContinent() + => _countries + .DistinctBy(c => c.Continent) + .ToDictionary(c => c.Continent); + + public static ILookup CallToLookupOnContinents() + => _countries.ToLookup(c => c.Continent); } \ No newline at end of file