Skip to content

Commit

Permalink
Select_ResultType
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 10, 2024
1 parent ab3f627 commit 680c879
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,39 @@ public static JArray Select(this JArray source, NewtonsoftJsonParsingConfig conf
var queryable = ToQueryable(source, config);
return ToJArray(() => queryable.Select(config, selector, args));
}

/// <summary>
/// Projects each element of a sequence into a new class of type TResult.
/// Details see http://solutionizing.net/category/linq/
/// </summary>
/// <param name="source">The source <see cref="JArray"/></param>
/// <param name="config">The <see cref="ParsingConfig"/>.</param>
/// <param name="resultType">The result type.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>An <see cref="JArray"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JArray Select(this JArray source, NewtonsoftJsonParsingConfig config, Type resultType, string selector, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source, config);
return ToJArray(() => queryable.Select(config, resultType, selector, args));
}

/// <summary>
/// Projects each element of a sequence into a new class of type TResult.
/// Details see http://solutionizing.net/category/linq/
/// </summary>
/// <param name="source">The source <see cref="JArray"/></param>
/// <param name="resultType">The result type.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>An <see cref="JArray"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JArray Select(this JArray source, Type resultType, string selector, params object?[] args)
{
return Select(source, NewtonsoftJsonParsingConfig.Default, resultType, selector, args);
}
#endregion Select

#region Where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,38 +735,6 @@ public static JsonDocument OrderBy(this JsonDocument source, string ordering, IC
}
#endregion OrderBy

#region Select
/// <summary>
/// Projects each element of a sequence into a new form.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. </param>
/// <returns>An <see cref="JsonDocument"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JsonDocument Select(this JsonDocument source, string selector, params object?[] args)
{
return Select(source, SystemTextJsonParsingConfig.Default, selector, args);
}

/// <summary>
/// Projects each element of a sequence into a new form.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>An <see cref="JsonElement"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JsonDocument Select(this JsonDocument source, SystemTextJsonParsingConfig config, string selector, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotNullOrEmpty(selector);

var queryable = ToQueryable(source, config);
return ToJsonDocumentArray(() => queryable.Select(config, selector, args));
}
#endregion Select

#region Page/PageResult
/// <summary>
/// Returns the elements as paged.
Expand Down Expand Up @@ -815,6 +783,71 @@ public static JsonDocument Reverse(this JsonDocument source)
}
#endregion Reverse

#region Select
/// <summary>
/// Projects each element of a sequence into a new form.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. </param>
/// <returns>An <see cref="JsonDocument"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JsonDocument Select(this JsonDocument source, string selector, params object?[] args)
{
return Select(source, SystemTextJsonParsingConfig.Default, selector, args);
}

/// <summary>
/// Projects each element of a sequence into a new form.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>An <see cref="JsonElement"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JsonDocument Select(this JsonDocument source, SystemTextJsonParsingConfig config, string selector, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotNullOrEmpty(selector);

var queryable = ToQueryable(source, config);
return ToJsonDocumentArray(() => queryable.Select(config, selector, args));
}

/// <summary>
/// Projects each element of a sequence into a new class of type TResult.
/// Details see http://solutionizing.net/category/linq/
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="config">The <see cref="ParsingConfig"/>.</param>
/// <param name="resultType">The result type.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>An <see cref="JsonDocument"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JsonDocument Select(this JsonDocument source, SystemTextJsonParsingConfig config, Type resultType, string selector, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source, config);
return ToJsonDocumentArray(() => queryable.Select(config, resultType, selector, args));
}

/// <summary>
/// Projects each element of a sequence into a new class of type TResult.
/// Details see http://solutionizing.net/category/linq/
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="resultType">The result type.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>An <see cref="JsonDocument"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JsonDocument Select(this JsonDocument source, Type resultType, string selector, params object?[] args)
{
return Select(source, SystemTextJsonParsingConfig.Default, resultType, selector, args);
}
#endregion Select

#region Where
/// <summary>
/// Filters a sequence of values based on a predicate.
Expand Down
11 changes: 5 additions & 6 deletions src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,7 @@ public static IQueryable Select(this IQueryable source, ParsingConfig config, st
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotEmpty(selector, nameof(selector));
Check.NotEmpty(selector);

bool createParameterCtor = config.EvaluateGroupByAtDatabase || SupportsLinqToObjects(config, source);
LambdaExpression lambda = DynamicExpressionParser.ParseLambda(config, createParameterCtor, source.ElementType, null, selector, args);
Expand Down Expand Up @@ -1796,7 +1796,7 @@ public static IQueryable<TResult> Select<TResult>(this IQueryable source, Parsin
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotEmpty(selector, nameof(selector));
Check.NotEmpty(selector);

bool createParameterCtor = config.EvaluateGroupByAtDatabase || SupportsLinqToObjects(config, source);
LambdaExpression lambda = DynamicExpressionParser.ParseLambda(config, createParameterCtor, source.ElementType, typeof(TResult), selector, args);
Expand Down Expand Up @@ -1840,7 +1840,7 @@ public static IQueryable Select(this IQueryable source, ParsingConfig config, Ty
Check.NotNull(source);
Check.NotNull(config);
Check.NotNull(resultType, nameof(resultType));
Check.NotEmpty(selector, nameof(selector));
Check.NotEmpty(selector);

bool createParameterCtor = config.EvaluateGroupByAtDatabase || SupportsLinqToObjects(config, source);
LambdaExpression lambda = DynamicExpressionParser.ParseLambda(config, createParameterCtor, source.ElementType, resultType, selector, args);
Expand All @@ -1858,7 +1858,6 @@ public static IQueryable Select(this IQueryable source, Type resultType, string
{
return Select(source, ParsingConfig.Default, resultType, selector, args);
}

#endregion Select

#region SelectMany
Expand Down Expand Up @@ -1905,7 +1904,7 @@ public static IQueryable SelectMany(this IQueryable source, ParsingConfig config
Check.NotNull(source);
Check.NotNull(config);
Check.NotNull(resultType, nameof(resultType));
Check.NotEmpty(selector, nameof(selector));
Check.NotEmpty(selector);

return SelectManyInternal(source, config, resultType, selector, args);
}
Expand Down Expand Up @@ -1976,7 +1975,7 @@ public static IQueryable<TResult> SelectMany<TResult>(this IQueryable source, Pa
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotEmpty(selector, nameof(selector));
Check.NotEmpty(selector);

bool createParameterCtor = config.EvaluateGroupByAtDatabase || SupportsLinqToObjects(config, source);
LambdaExpression lambda = DynamicExpressionParser.ParseLambda(createParameterCtor, source.ElementType, null, selector, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,21 @@ public void Select()
array.Should().BeEquivalentTo("John", "Doe");
}

[Fact]
public void Select_ResultType()
{
// Arrange
var json = @"[1, 2, 3]";
var source = JArray.Parse(json);

// Act
var result = source.Select(typeof(int), "it * it");

// Assert
var array = result.Select(x => x.Value<int>());
array.Should().ContainInOrder(1, 4, 9);
}

[Fact]
public void Where_Select()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.IO;
using System.Text.Json;
using FluentAssertions;
using Xunit;

Expand Down Expand Up @@ -239,6 +240,21 @@ public void Select()
array.Should().BeEquivalentTo("John", "Doe");
}

[Fact]
public void Select_ResultType()
{
// Arrange
var json = @"[1, 2, 3]";
var source = JsonDocument.Parse(json);

// Act
var result = source.Select(typeof(int), "it * it");

// Assert
var array = result.RootElement.EnumerateArray().Select(x => x.GetInt32());
array.Should().ContainInOrder(1, 4, 9);
}

[Fact]
public void Where_Select()
{
Expand Down

0 comments on commit 680c879

Please sign in to comment.