From 775788a08e28dc11ac94f563d8f2d5d92a083af8 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Wed, 10 Apr 2024 11:42:00 +0200 Subject: [PATCH] min max --- .../NewtonsoftJsonExtensions.cs | 20 ++--- .../SystemTextJsonExtensions.cs | 87 ++++++++++++++++--- .../NewtonsoftJsonTests.cs | 4 +- .../SystemTextJsonTests.cs | 4 +- 4 files changed, 87 insertions(+), 28 deletions(-) diff --git a/src/System.Linq.Dynamic.Core.NewtonsoftJson/NewtonsoftJsonExtensions.cs b/src/System.Linq.Dynamic.Core.NewtonsoftJson/NewtonsoftJsonExtensions.cs index eb2fafba..4604da77 100644 --- a/src/System.Linq.Dynamic.Core.NewtonsoftJson/NewtonsoftJsonExtensions.cs +++ b/src/System.Linq.Dynamic.Core.NewtonsoftJson/NewtonsoftJsonExtensions.cs @@ -474,13 +474,13 @@ public static JToken Last(this JArray source, LambdaExpression lambda) /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The max element in the sequence. - public static JToken Max(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args) + public static object Max(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); - return ToJToken(queryable.Max(config, predicate, args))!; + return queryable.Max(config, predicate, args); } /// @@ -490,7 +490,7 @@ public static JToken Max(this JArray source, NewtonsoftJsonParsingConfig config, /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The max element in the sequence. - public static JToken Max(this JArray source, string predicate, params object?[] args) + public static object Max(this JArray source, string predicate, params object?[] args) { return Max(source, NewtonsoftJsonParsingConfig.Default, predicate, args); } @@ -501,12 +501,12 @@ public static JToken Max(this JArray source, string predicate, params object?[] /// A sequence of values to calculate find the max for. /// A Lambda Expression. /// The max element in the sequence. - public static JToken Max(this JArray source, LambdaExpression lambda) + public static object Max(this JArray source, LambdaExpression lambda) { Check.NotNull(source); var queryable = ToQueryable(source); - return ToJToken(queryable.Max(lambda))!; + return queryable.Max(lambda); } #endregion Max @@ -519,13 +519,13 @@ public static JToken Max(this JArray source, LambdaExpression lambda) /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The min element in the sequence. - public static JToken Min(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args) + public static object Min(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); - return ToJToken(queryable.Min(config, predicate, args))!; + return queryable.Min(config, predicate, args); } /// @@ -535,7 +535,7 @@ public static JToken Min(this JArray source, NewtonsoftJsonParsingConfig config, /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The min element in the sequence. - public static JToken Min(this JArray source, string predicate, params object?[] args) + public static object Min(this JArray source, string predicate, params object?[] args) { return Min(source, NewtonsoftJsonParsingConfig.Default, predicate, args); } @@ -546,12 +546,12 @@ public static JToken Min(this JArray source, string predicate, params object?[] /// A sequence of values to calculate find the min for. /// A Lambda Expression. /// The min element in the sequence. - public static JToken Min(this JArray source, LambdaExpression lambda) + public static object Min(this JArray source, LambdaExpression lambda) { Check.NotNull(source); var queryable = ToQueryable(source); - return ToJToken(queryable.Min(lambda))!; + return queryable.Min(lambda); } #endregion Min diff --git a/src/System.Linq.Dynamic.Core.SystemTextJson/SystemTextJsonExtensions.cs b/src/System.Linq.Dynamic.Core.SystemTextJson/SystemTextJsonExtensions.cs index 64755397..3d4c117a 100644 --- a/src/System.Linq.Dynamic.Core.SystemTextJson/SystemTextJsonExtensions.cs +++ b/src/System.Linq.Dynamic.Core.SystemTextJson/SystemTextJsonExtensions.cs @@ -565,12 +565,12 @@ public static JsonElement Last(this JsonDocument source, LambdaExpression lambda /// /// A sequence of values to calculate find the max for. /// The max element in the sequence. - public static JsonElement Max(this JsonDocument source) + public static object Max(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); - return ToJsonElement(queryable.Max()) ?? default; + return queryable.Max(); } /// @@ -581,13 +581,13 @@ public static JsonElement Max(this JsonDocument source) /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The max element in the sequence. - public static JsonElement Max(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) + public static object Max(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); - return ToJsonElement(queryable.Max(config, predicate, args)) ?? default; + return queryable.Max(config, predicate, args); } /// @@ -597,7 +597,7 @@ public static JsonElement Max(this JsonDocument source, SystemTextJsonParsingCon /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The max element in the sequence. - public static JsonElement Max(this JsonDocument source, string predicate, params object?[] args) + public static object Max(this JsonDocument source, string predicate, params object?[] args) { return Max(source, SystemTextJsonParsingConfig.Default, predicate, args); } @@ -608,12 +608,12 @@ public static JsonElement Max(this JsonDocument source, string predicate, params /// A sequence of values to calculate find the max for. /// A Lambda Expression. /// The max element in the sequence. - public static JsonElement Max(this JsonDocument source, LambdaExpression lambda) + public static object Max(this JsonDocument source, LambdaExpression lambda) { Check.NotNull(source); var queryable = ToQueryable(source); - return ToJsonElement(queryable.Max(lambda)) ?? default; + return queryable.Max(lambda); } #endregion Max @@ -623,12 +623,12 @@ public static JsonElement Max(this JsonDocument source, LambdaExpression lambda) /// /// A sequence of values to calculate find the min for. /// The min element in the sequence. - public static JsonElement Min(this JsonDocument source) + public static object Min(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); - return ToJsonElement(queryable.Min()) ?? default; + return queryable.Min(); } /// @@ -639,13 +639,13 @@ public static JsonElement Min(this JsonDocument source) /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The min element in the sequence. - public static JsonElement Min(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) + public static object Min(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); - return ToJsonElement(queryable.Min(config, predicate, args)) ?? default; + return queryable.Min(config, predicate, args); } /// @@ -655,7 +655,7 @@ public static JsonElement Min(this JsonDocument source, SystemTextJsonParsingCon /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The min element in the sequence. - public static JsonElement Min(this JsonDocument source, string predicate, params object?[] args) + public static object Min(this JsonDocument source, string predicate, params object?[] args) { return Min(source, SystemTextJsonParsingConfig.Default, predicate, args); } @@ -666,12 +666,12 @@ public static JsonElement Min(this JsonDocument source, string predicate, params /// A sequence of values to calculate find the min for. /// A Lambda Expression. /// The min element in the sequence. - public static JsonElement Min(this JsonDocument source, LambdaExpression lambda) + public static object Min(this JsonDocument source, LambdaExpression lambda) { Check.NotNull(source); var queryable = ToQueryable(source); - return ToJsonElement(queryable.Min(lambda)) ?? default; + return queryable.Min(lambda); } #endregion Min @@ -1018,6 +1018,65 @@ public static JsonDocument SkipWhile(this JsonDocument source, string predicate, } #endregion SkipWhile + #region Sum + /// + /// Computes the sum of a sequence of numeric values. + /// + /// A sequence of numeric values to calculate the sum of. + /// The sum of the values in the sequence. + public static object Sum(this JsonDocument source) + { + Check.NotNull(source); + + var queryable = ToQueryable(source); + return queryable.Sum(); + } + + /// + /// Computes the sum of a sequence of numeric values. + /// + /// A sequence of numeric values to calculate the sum of. + /// The . + /// A function to test each element for a condition. + /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. + /// The sum of the values in the sequence. + public static object Sum(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) + { + Check.NotNull(source); + Check.NotNull(config); + + var queryable = ToQueryable(source, config); + return queryable.Sum(predicate, args); + } + + /// + /// Computes the sum of a sequence of numeric values. + /// + /// A sequence of numeric values to calculate the sum of. + /// A function to test each element for a condition. + /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. + /// The sum of the values in the sequence. + public static object Sum(this JsonDocument source, string predicate, params object?[] args) + { + return Sum(source, SystemTextJsonParsingConfig.Default, predicate, args); + } + + /// + /// Computes the sum of a sequence of numeric values. + /// + /// A sequence of numeric values to calculate the sum of. + /// A Lambda Expression. + /// The sum of the values in the sequence. + public static object Sum(this JsonDocument source, LambdaExpression lambda) + { + Check.NotNull(source); + Check.NotNull(lambda); + + var queryable = ToQueryable(source); + return queryable.Sum(lambda); + } + #endregion Sum + #region Where /// /// Filters a sequence of values based on a predicate. diff --git a/test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs b/test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs index d8fe839f..84cd5813 100644 --- a/test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs +++ b/test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs @@ -154,14 +154,14 @@ public void LastOrDefault() public void Max() { // Act + Assert - ((string?)_source.Max("Age")).Should().Be("40"); + ((int?)_source.Max("Age")).Should().Be(40); } [Fact] public void Min() { // Act + Assert - ((string?)_source.Min("Age")).Should().Be("30"); + ((int?)_source.Min("Age")).Should().Be(30); } [Fact] diff --git a/test/System.Linq.Dynamic.Core.SystemTextJson.Tests/SystemTextJsonTests.cs b/test/System.Linq.Dynamic.Core.SystemTextJson.Tests/SystemTextJsonTests.cs index cb4cba33..231f3237 100644 --- a/test/System.Linq.Dynamic.Core.SystemTextJson.Tests/SystemTextJsonTests.cs +++ b/test/System.Linq.Dynamic.Core.SystemTextJson.Tests/SystemTextJsonTests.cs @@ -166,14 +166,14 @@ public void LastOrDefault() public void Max() { // Act + Assert - _source.Max("Age").GetRawText().Should().BeEquivalentTo("40"); + _source.Max("Age").Should().BeEquivalentTo(40); } [Fact] public void Min() { // Act + Assert - _source.Min("Age").GetRawText().Should().BeEquivalentTo("30"); + _source.Min("Age").Should().BeEquivalentTo(30); } [Fact]