Exemple #1
0
        public void CanAggregateIntoImmutableDictionary()
        {
            ImmutableDictionary <int, string> stringRepByValue = from i in INT_SOURCE
                                                                 where Distinct
                                                                 group i.ToString() by ToImmutableDictionary(i) into dict
                                                                 select dict;

            stringRepByValue.GetType().Should().Be <ImmutableDictionary <int, string> >();
            stringRepByValue.Should().Contain(new Dictionary <int, string> {
                { 1, "1" },
                { 9, "9" },
                { 4, "4" },
                { 5, "5" },
                { 0, "0" },
                { 8, "8" },
                { 7, "7" }
            });

            ImmutableDictionary <int, string> stringRepByValue2 = from i in INT_SOURCE
                                                                  where Distinct
                                                                  group i by ToImmutableDictionary(i, i.ToString()) into dict
                                                                  select dict;

            stringRepByValue2.GetType().Should().Be <ImmutableDictionary <int, string> >();
            stringRepByValue2.Should().Contain(new Dictionary <int, string> {
                { 1, "1" },
                { 9, "9" },
                { 4, "4" },
                { 5, "5" },
                { 0, "0" },
                { 8, "8" },
                { 7, "7" }
            });
        }
        public void WriteImmutableDictionaryOfInt32()
        {
            ImmutableDictionary <int, int> value = ImmutableDictionary.CreateRange(new Dictionary <int, int>
            {
                { 1, 1 },
                { 2, 2 },
                { 3, 3 }
            });
            const string expected = @"{""1"":1,""2"":2,""3"":3}";

            JsonSerializerOptions options = new JsonSerializerOptions();

            options.SetupExtensions();
            string actual = JsonSerializer.Serialize(value, value.GetType(), options);

            Assert.Equal(expected, actual);
        }