Skip to content

Commit

Permalink
use collection expression
Browse files Browse the repository at this point in the history
  • Loading branch information
6bee committed Jun 8, 2024
1 parent c34148a commit eae3873
Show file tree
Hide file tree
Showing 37 changed files with 107 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ public async ValueTask<TResult> ExecuteAsync<TResult>(SystemLinq.Expression expr
private TResult MapAsyncEnumerable<TResult>(Type elementType, IAsyncEnumerable<TSource?> source, CancellationToken cancellation)
{
var method = _mapAsyncEnumerableMethodInfo.MakeGenericMethod(elementType);
var result = method.Invoke(null, new object[] { source, _resultMapper, cancellation });
var result = method.Invoke(null, [source, _resultMapper, cancellation]);
return (TResult)result!;
}

private ValueTask<TResult> MapSingleElementStream<TResult>(IAsyncEnumerable<TSource?> source, CancellationToken cancellation)
{
var method = _mapSingleElementStreamMethodInfo.MakeGenericMethod(typeof(TResult));
var result = method.Invoke(null, new object[] { source, _resultMapper, cancellation });
var result = method.Invoke(null, [source, _resultMapper, cancellation]);
return (ValueTask<TResult>)result!;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ protected override bool ShouldMapToDynamicObject(IEnumerable collection)
var genericTypeArguments = default(Type[]);
if (obj?.GetType().Implements(typeof(IAsyncGrouping<,>), out genericTypeArguments) is true)
{
obj = MapAsyncGroupToDynamicObjectGraphMethod(genericTypeArguments!).Invoke(null, new[] { obj });
obj = MapAsyncGroupToDynamicObjectGraphMethod(genericTypeArguments!).Invoke(null, [obj]);
}
else if (obj?.GetType().Implements(typeof(IAsyncEnumerable<>), out genericTypeArguments) is true)
{
obj = MapAsyncEnumerableMethod(genericTypeArguments!).Invoke(null, new[] { obj });
obj = MapAsyncEnumerableMethod(genericTypeArguments!).Invoke(null, [obj]);
}

return base.MapToDynamicObjectGraph(obj, setTypeInformation);
Expand Down
6 changes: 3 additions & 3 deletions src/Remote.Linq.Async.Queryable/ExpressionExecution/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static class Helper
{
task.AssertNotNull();
resultType.AssertNotNull();
var result = ToSingleElementStreamMethod(resultType).Invoke(null, new[] { task });
var result = ToSingleElementStreamMethod(resultType).Invoke(null, [task]);
return (IAsyncEnumerable<object?>)result!;
}

Expand All @@ -36,7 +36,7 @@ internal static class Helper
public static IAsyncEnumerable<object?> MapAsyncEnumerable(object asyncEnumerable, Type itemType)
{
itemType.AssertNotNull();
var result = MapAsyncEnumerableMethod(itemType).Invoke(null, new[] { asyncEnumerable });
var result = MapAsyncEnumerableMethod(itemType).Invoke(null, [asyncEnumerable]);
return (IAsyncEnumerable<object?>)result!;
}

Expand All @@ -55,7 +55,7 @@ internal static class Helper
{
task.AssertNotNull();
resultType.AssertNotNull();
var result = MapTaskAsyncMethod(resultType).Invoke(null, new[] { task });
var result = MapTaskAsyncMethod(resultType).Invoke(null, [task]);
return (ValueTask<object?>)result!;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Remote.Linq.EntityFramework/EntityFrameworkMethodInfos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ private TProperty()

internal static readonly MethodInfo StringIncludeMethodInfo = typeof(System.Data.Entity.QueryableExtensions).GetMethodEx(
nameof(System.Data.Entity.QueryableExtensions.Include),
new[] { typeof(T) },
[typeof(T)],
typeof(IQueryable<T>),
typeof(string));

internal static readonly MethodInfo IncludeMethodInfo = typeof(System.Data.Entity.QueryableExtensions).GetMethodEx(
nameof(System.Data.Entity.QueryableExtensions.Include),
new[] { typeof(T), typeof(TProperty) },
[typeof(T), typeof(TProperty)],
typeof(IQueryable<T>),
typeof(Expression<Func<T, TProperty>>));
}
6 changes: 3 additions & 3 deletions src/Remote.Linq.EntityFramework/ExpressionExecution/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ private TEntity()

private static readonly MethodInfo QueryableToListAsyncMethod = typeof(QueryableExtensions).GetMethodEx(
nameof(QueryableExtensions.ToListAsync),
new[] { typeof(TSource) },
[typeof(TSource)],
typeof(IQueryable<TSource>),
typeof(CancellationToken));

private static readonly MethodInfo DbContextSetMethod = typeof(DbContext).GetMethodEx(
nameof(DbContext.Set),
genericArguments: new[] { typeof(TEntity) });
genericArguments: [typeof(TEntity)]);

internal static Task ToListAsync(IQueryable source, CancellationToken cancellation)
{
source.AssertNotNull();
var method = QueryableToListAsyncMethod.MakeGenericMethod(source.ElementType);
var task = method.Invoke(null, new object[] { source, cancellation });
var task = method.Invoke(null, [source, cancellation]);
return (Task)task!;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ private TEntity()

private static readonly MethodInfo _entityFrameworkQueryableToListAsyncMethod = typeof(EntityFrameworkQueryableExtensions).GetMethodEx(
nameof(EntityFrameworkQueryableExtensions.ToListAsync),
new[] { typeof(TSource) },
[typeof(TSource)],
typeof(IQueryable<TSource>),
typeof(CancellationToken));

private static readonly MethodInfo _dbContextSetMethod = typeof(DbContext).GetMethodEx(
nameof(DbContext.Set),
genericArguments: new[] { typeof(TEntity) });
genericArguments: [typeof(TEntity)]);

private static readonly MethodInfo _executeAsAsyncStreamMethod = typeof(Helper).GetMethodEx(nameof(ExecuteAsAsyncStream));

internal static Task ToListAsync(IQueryable source, CancellationToken cancellation)
{
source.AssertNotNull();
var method = _entityFrameworkQueryableToListAsyncMethod.MakeGenericMethod(source.ElementType);
var task = method.Invoke(null, new object[] { source, cancellation });
var task = method.Invoke(null, [source, cancellation]);
return (Task)task!;
}

Expand Down Expand Up @@ -88,7 +88,7 @@ public IQueryable GetQueryableSet(Type type)
{
queryable.AssertNotNull();
var method = _executeAsAsyncStreamMethod.MakeGenericMethod(queryable.ElementType);
var asyncStream = method.Invoke(null, new object[] { queryable, cancellation });
var asyncStream = method.Invoke(null, [queryable, cancellation]);
return (IAsyncEnumerable<object?>)asyncStream!;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ private TPreviousProperty()

internal static readonly MethodInfo StringIncludeMethodInfo = typeof(EntityFrameworkQueryableExtensions).GetMethodEx(
nameof(EntityFrameworkQueryableExtensions.Include),
new[] { typeof(TEntity) },
[typeof(TEntity)],
typeof(IQueryable<TEntity>),
typeof(string));

internal static readonly MethodInfo IncludeMethodInfo = typeof(EntityFrameworkQueryableExtensions).GetMethodEx(
nameof(EntityFrameworkQueryableExtensions.Include),
new[] { typeof(TEntity), typeof(TProperty) },
[typeof(TEntity), typeof(TProperty)],
typeof(IQueryable<TEntity>),
typeof(Expression<Func<TEntity, TProperty>>));

internal static readonly MethodInfo ThenIncludeAfterEnumerableMethodInfo = typeof(EntityFrameworkQueryableExtensions).GetMethodEx(
nameof(EntityFrameworkQueryableExtensions.ThenInclude),
new[] { typeof(TEntity), typeof(TPreviousProperty), typeof(TProperty) },
[typeof(TEntity), typeof(TPreviousProperty), typeof(TProperty)],
typeof(Microsoft.EntityFrameworkCore.Query.IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>>),
typeof(Expression<Func<TPreviousProperty, TProperty>>));

internal static readonly MethodInfo ThenIncludeAfterReferenceMethodInfo = typeof(EntityFrameworkQueryableExtensions).GetMethodEx(
nameof(EntityFrameworkQueryableExtensions.ThenInclude),
new[] { typeof(TEntity), typeof(TPreviousProperty), typeof(TProperty) },
[typeof(TEntity), typeof(TPreviousProperty), typeof(TProperty)],
typeof(Microsoft.EntityFrameworkCore.Query.IIncludableQueryable<TEntity, TPreviousProperty>),
typeof(Expression<Func<TPreviousProperty, TProperty>>));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Remote.Linq/DynamicQuery/AsyncRemoteQueryProvider`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Remote.Linq.DynamicQuery;
public sealed class AsyncRemoteQueryProvider<TSource> : IAsyncRemoteQueryProvider
{
private static readonly MethodInfo _executeMethod = typeof(AsyncRemoteQueryProvider<TSource>)
.GetMethodEx(nameof(Execute), new[] { typeof(MethodInfos.TResult) }, typeof(SystemLinq.Expression));
.GetMethodEx(nameof(Execute), [typeof(MethodInfos.TResult)], typeof(SystemLinq.Expression));

private readonly Func<RemoteLinq.Expression, CancellationToken, ValueTask<TSource?>> _asyncDataProvider;
private readonly IAsyncQueryResultMapper<TSource> _resultMapper;
Expand Down
2 changes: 1 addition & 1 deletion src/Remote.Linq/DynamicQuery/DynamicResultMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal static TResult MapToSingleResult<TResult>(Type elementType, System.Coll
var hasPredicate = methodCallExpression.Arguments.Count is 2;
var arguments = hasPredicate
? new object[] { result, GetTruePredicate(elementType) }
: new object[] { result };
: [result];
var method = methodCallExpression.Method.Name.EndsWith("OrDefault", StringComparison.Ordinal)
? (hasPredicate ? MethodInfos.Enumerable.SingleOrDefaultWithPredicate : MethodInfos.Enumerable.SingleOrDefault)
: (hasPredicate ? MethodInfos.Enumerable.SingleWithPredicate : MethodInfos.Enumerable.Single);
Expand Down
2 changes: 1 addition & 1 deletion src/Remote.Linq/DynamicQuery/RemoteQueryProvider`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Remote.Linq.DynamicQuery;
public sealed class RemoteQueryProvider<TSource> : IRemoteQueryProvider
{
private static readonly MethodInfo _executeMethod = typeof(RemoteQueryProvider<TSource>)
.GetMethodEx(nameof(Execute), new[] { typeof(MethodInfos.TResult) }, typeof(Expression));
.GetMethodEx(nameof(Execute), [typeof(MethodInfos.TResult)], typeof(Expression));

private readonly Func<Expressions.Expression, TSource?> _dataProvider;
private readonly IQueryResultMapper<TSource> _resultMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private DynamicObject Map(object? value, Type? type)
: _mapper.MapObject(value, _setTypeInformation);

private object[] MapArray(object array, Type elementType)
=> (object[])_mapArrayMethodInfo.MakeGenericMethod(elementType).Invoke(this, new[] { array })!;
=> (object[])_mapArrayMethodInfo.MakeGenericMethod(elementType).Invoke(this, [array])!;

private object[] MapArray<T>(T[] array)
=> array
Expand Down
16 changes: 8 additions & 8 deletions src/Remote.Linq/ExpressionTranslatorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ protected override bool ShouldMapToDynamicObject(IEnumerable collection)
typeof(System.Numerics.Complex),
typeof(byte[]),
}
.SelectMany(x => x.IsValueType ? new[] { x, typeof(Nullable<>).MakeGenericType(x) } : new[] { x })
.SelectMany(x => x.IsValueType ? new[] { x, typeof(Nullable<>).MakeGenericType(x) } : [x])
.ToHashSet()
.Contains;

private static readonly Type[] _unmappedTypes = new[]
{
private static readonly Type[] _unmappedTypes =
[
typeof(CancellationToken),
typeof(ConstantQueryArgument),
typeof(VariableQueryArgument),
Expand All @@ -149,12 +149,12 @@ protected override bool ShouldMapToDynamicObject(IEnumerable collection)
typeof(SystemLinq.Expression),
typeof(IQueryable),
typeof(IRemoteLinqQueryable),
};
];

private static readonly Type[] _excludeFromUnmappedTypes = new[]
{
private static readonly Type[] _excludeFromUnmappedTypes =
[
typeof(EnumerableQuery),
};
];

/// <summary>
/// Initializes a new instance of the <see cref="ExpressionTranslatorContext"/> class.
Expand Down Expand Up @@ -238,7 +238,7 @@ public ExpressionTranslatorContext(
private static object? MapGroupToDynamicObjectGraphMethod(Type[] genericTypeArguments, object group)
=> _mapGroupToDynamicObjectGraphMethodDefinition
.MakeGenericMethod(genericTypeArguments)
.Invoke(null, new[] { group });
.Invoke(null, [group]);

private static Grouping<TKey, TElement> MapGroupToDynamicObjectGraph<TKey, TElement>(IGrouping<TKey, TElement> group)
=> group is Grouping<TKey, TElement> grouping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,20 @@ private static Expression Evaluate(Expression expression)
var elementType = TypeHelper.GetElementType(collectionType);
if (expression.Type.IsAssignableFrom(elementType.MakeArrayType()))
{
var enumerated = MethodInfos.Enumerable.ToArray.MakeGenericMethod(elementType).Invoke(null, new[] { value });
var enumerated = MethodInfos.Enumerable.ToArray.MakeGenericMethod(elementType).Invoke(null, [value]);
value = enumerated;
}
else if (value is EnumerableQuery && expression.Type.IsAssignableFrom(typeof(IQueryable<>).MakeGenericType(elementType)))
{
var enumerated = MethodInfos.Enumerable.ToArray.MakeGenericMethod(elementType).Invoke(null, new[] { value });
var queryable = MethodInfos.Queryable.AsQueryable.MakeGenericMethod(elementType).Invoke(null, new[] { enumerated });
var enumerated = MethodInfos.Enumerable.ToArray.MakeGenericMethod(elementType).Invoke(null, [value]);
var queryable = MethodInfos.Queryable.AsQueryable.MakeGenericMethod(elementType).Invoke(null, [enumerated]);
value = queryable;
}
}

return Expression.Property(
Expression.New(
typeof(VariableQueryArgument<>).MakeGenericType(expression.Type).GetConstructor(new[] { expression.Type })!,
typeof(VariableQueryArgument<>).MakeGenericType(expression.Type).GetConstructor([expression.Type])!,
Expression.Constant(value, expression.Type)),
nameof(VariableQueryArgument<object>.Value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected override Expression VisitConstant(ConstantExpression node)
{
var type = nonGenericQueryArgument.Type.ResolveType(TypeResolver);
var value = nonGenericQueryArgument.Value;
var queryArgument = Activator.CreateInstance(typeof(VariableQueryArgument<>).MakeGenericType(type), new[] { value });
var queryArgument = Activator.CreateInstance(typeof(VariableQueryArgument<>).MakeGenericType(type), [value]);
return new ConstantExpression(queryArgument);
}

Expand All @@ -136,7 +136,7 @@ protected override Expression VisitConstant(ConstantExpression node)
var elementType = nonGenericQueryArgumentList.ElementType.ResolveType(TypeResolver);
var values = nonGenericQueryArgumentList.Values;
var methodInfo = CreateVariableQueryArgumentListMethodInfo.MakeGenericMethod(elementType);
var queryArgument = methodInfo.Invoke(null, new object[] { values });
var queryArgument = methodInfo.Invoke(null, [values]);
return new ConstantExpression(queryArgument);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Remote.Linq/Include/IncludeQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private sealed class TPreviousProperty
/// </summary>
public static readonly MethodInfo StringIncludeMethodInfo = typeof(IncludeQueryableExtensions).GetMethodEx(
nameof(IncludeQueryableExtensions.Include),
new[] { typeof(T) },
[typeof(T)],
typeof(IQueryable<T>),
typeof(string));

Expand All @@ -52,7 +52,7 @@ private sealed class TPreviousProperty
/// </summary>
public static readonly MethodInfo IncludeMethodInfo = typeof(IncludeQueryableExtensions).GetMethodEx(
nameof(IncludeQueryableExtensions.Include),
new[] { typeof(T), typeof(TProperty) },
[typeof(T), typeof(TProperty)],
typeof(IQueryable<T>),
typeof(Expression<Func<T, TProperty>>));

Expand All @@ -63,7 +63,7 @@ private sealed class TPreviousProperty
/// </summary>
public static readonly MethodInfo ThenIncludeAfterEnumerableMethodInfo = typeof(IncludeQueryableExtensions).GetMethodEx(
nameof(IncludeQueryableExtensions.ThenInclude),
new[] { typeof(T), typeof(TPreviousProperty), typeof(TProperty) },
[typeof(T), typeof(TPreviousProperty), typeof(TProperty)],
typeof(IIncludableQueryable<T, IEnumerable<TPreviousProperty>>),
typeof(Expression<Func<TPreviousProperty, TProperty>>));

Expand All @@ -74,7 +74,7 @@ private sealed class TPreviousProperty
/// </summary>
public static readonly MethodInfo ThenIncludeAfterReferenceMethodInfo = typeof(IncludeQueryableExtensions).GetMethodEx(
nameof(IncludeQueryableExtensions.ThenInclude),
new[] { typeof(T), typeof(TPreviousProperty), typeof(TProperty) },
[typeof(T), typeof(TPreviousProperty), typeof(TProperty)],
typeof(IIncludableQueryable<T, TPreviousProperty>),
typeof(Expression<Func<TPreviousProperty, TProperty>>));

Expand Down
2 changes: 1 addition & 1 deletion src/Remote.Linq/Include/RemoteIncludeExpressionReWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected override Expression VisitConstant(ConstantExpression node)
};

var m = method.MakeGenericMethod(args);
var expression = (Expression)m.Invoke(null, new[] { node.Value })!;
var expression = (Expression)m.Invoke(null, [node.Value])!;
return Visit(expression);
}

Expand Down
Loading

0 comments on commit eae3873

Please sign in to comment.