|
2 | 2 | using System.Linq.Dynamic.Core.NewtonsoftJson.Config; |
3 | 3 | using System.Linq.Dynamic.Core.NewtonsoftJson.Extensions; |
4 | 4 | using System.Linq.Dynamic.Core.Validation; |
| 5 | +using JetBrains.Annotations; |
5 | 6 | using Newtonsoft.Json.Linq; |
6 | 7 |
|
7 | 8 | namespace System.Linq.Dynamic.Core.NewtonsoftJson; |
@@ -303,6 +304,42 @@ public static JToken First(this JArray source, string predicate, params object?[ |
303 | 304 | } |
304 | 305 | #endregion FirstOrDefault |
305 | 306 |
|
| 307 | + #region GroupBy |
| 308 | + /// <summary> |
| 309 | + /// Groups the elements of a sequence according to a specified key string function |
| 310 | + /// and creates a result value from each group and its key. |
| 311 | + /// </summary> |
| 312 | + /// <param name="source">A <see cref="JArray"/> whose elements to group.</param> |
| 313 | + /// <param name="keySelector">A string expression to specify the key for each element.</param> |
| 314 | + /// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param> |
| 315 | + /// <returns>A <see cref="JArray"/> where each element represents a projection over a group and its key.</returns> |
| 316 | + [PublicAPI] |
| 317 | + public static JArray GroupBy(this JArray source, string keySelector, params object[]? args) |
| 318 | + { |
| 319 | + return GroupBy(source, NewtonsoftJsonParsingConfig.Default, keySelector, args); |
| 320 | + } |
| 321 | + |
| 322 | + /// <summary> |
| 323 | + /// Groups the elements of a sequence according to a specified key string function |
| 324 | + /// and creates a result value from each group and its key. |
| 325 | + /// </summary> |
| 326 | + /// <param name="source">A <see cref="JArray"/> whose elements to group.</param> |
| 327 | + /// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</param> |
| 328 | + /// <param name="keySelector">A string expression to specify the key for each element.</param> |
| 329 | + /// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param> |
| 330 | + /// <returns>A <see cref="JArray"/> where each element represents a projection over a group and its key.</returns> |
| 331 | + [PublicAPI] |
| 332 | + public static JArray GroupBy(this JArray source, NewtonsoftJsonParsingConfig config, string keySelector, params object[]? args) |
| 333 | + { |
| 334 | + Check.NotNull(source); |
| 335 | + Check.NotNull(config); |
| 336 | + Check.NotNullOrEmpty(keySelector); |
| 337 | + |
| 338 | + var queryable = ToQueryable(source, config); |
| 339 | + return ToJArray(() => queryable.GroupBy(config, keySelector, args)); |
| 340 | + } |
| 341 | + #endregion |
| 342 | + |
306 | 343 | #region Last |
307 | 344 | /// <summary> |
308 | 345 | /// Returns the last element of a sequence that satisfies a specified condition. |
@@ -813,7 +850,17 @@ private static JArray ToJArray(Func<IQueryable> func) |
813 | 850 | var array = new JArray(); |
814 | 851 | foreach (var dynamicElement in func()) |
815 | 852 | { |
816 | | - var element = dynamicElement is DynamicClass dynamicClass ? JObject.FromObject(dynamicClass) : dynamicElement; |
| 853 | + var element = dynamicElement switch |
| 854 | + { |
| 855 | + IGrouping<object, object> grouping => new JObject |
| 856 | + { |
| 857 | + [nameof(grouping.Key)] = JToken.FromObject(grouping.Key), |
| 858 | + ["Values"] = ToJArray(grouping.AsQueryable) |
| 859 | + }, |
| 860 | + DynamicClass dynamicClass => JObject.FromObject(dynamicClass), |
| 861 | + _ => dynamicElement |
| 862 | + }; |
| 863 | + |
817 | 864 | array.Add(element); |
818 | 865 | } |
819 | 866 |
|
|
0 commit comments