Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SequenceEqual #860

Merged
merged 3 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@

if (newType != null)
{
return Expression.NewArrayInit(newType, expressions.Select(expression => _parsingConfig.ExpressionPromoter.Promote(expression, newType, true, true)));

Check warning on line 1499 in src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

View workflow job for this annotation

GitHub Actions / Linux: Build and Tests

Argument of type 'IEnumerable<Expression?>' cannot be used for parameter 'initializers' of type 'IEnumerable<Expression>' in 'NewArrayExpression Expression.NewArrayInit(Type type, IEnumerable<Expression> initializers)' due to differences in the nullability of reference types.

Check warning on line 1499 in src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

View workflow job for this annotation

GitHub Actions / Linux: Build and Tests

Argument of type 'IEnumerable<Expression?>' cannot be used for parameter 'initializers' of type 'IEnumerable<Expression>' in 'NewArrayExpression Expression.NewArrayInit(Type type, IEnumerable<Expression> initializers)' due to differences in the nullability of reference types.
}

return Expression.NewArrayInit(expressions.All(expression => expression.Type == expressions[0].Type) ? expressions[0].Type : typeof(object), expressions);
Expand Down Expand Up @@ -1564,7 +1564,7 @@
else
{
Type propertyType = constructorParameters[i].ParameterType;
string cParameterName = constructorParameters[i].Name;

Check warning on line 1567 in src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

View workflow job for this annotation

GitHub Actions / Linux: Build and Tests

Converting null literal or possible null value to non-nullable type.

Check warning on line 1567 in src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

View workflow job for this annotation

GitHub Actions / Linux: Build and Tests

Converting null literal or possible null value to non-nullable type.
var propertyAndIndex = properties.Select((p, index) => new { p, index })
.First(p => p.p.Name == cParameterName && (p.p.Type == propertyType || p.p.Type == Nullable.GetUnderlyingType(propertyType)));
// Promote from Type to Nullable Type if needed
Expand All @@ -1572,7 +1572,7 @@
}
}

return Expression.New(ctor, expressionsPromoted, (IEnumerable<MemberInfo>)propertyInfos);

Check warning on line 1575 in src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

View workflow job for this annotation

GitHub Actions / Linux: Build and Tests

Argument of type 'List<Expression?>' cannot be used for parameter 'arguments' of type 'IEnumerable<Expression>' in 'NewExpression Expression.New(ConstructorInfo constructor, IEnumerable<Expression>? arguments, IEnumerable<MemberInfo>? members)' due to differences in the nullability of reference types.
}
}

Expand All @@ -1586,7 +1586,7 @@
.Select((t, i) => _parsingConfig.ExpressionPromoter.Promote(expressions[i], t.ParameterType, true, true))
.ToArray();

return Expression.New(exactConstructor, expressionsPromoted);

Check warning on line 1589 in src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

View workflow job for this annotation

GitHub Actions / Linux: Build and Tests

Argument of type 'Expression?[]' cannot be used for parameter 'arguments' of type 'Expression[]' in 'NewExpression Expression.New(ConstructorInfo constructor, params Expression[]? arguments)' due to differences in the nullability of reference types.
}

// Option 2. Call the default (empty) constructor and set the members
Expand Down Expand Up @@ -1820,13 +1820,11 @@
var isStringWithStringMethod = type == typeof(string) && _methodFinder.ContainsMethod(type, id, isStaticAccess);
var isApplicableForEnumerable = !isStaticAccess && !isConstantString && !isStringWithStringMethod;

if (isApplicableForEnumerable && TypeHelper.TryFindGenericType(typeof(IEnumerable<>), type, out var enumerableType))
if (isApplicableForEnumerable &&
TypeHelper.TryFindGenericType(typeof(IEnumerable<>), type, out var enumerableType) &&
TryParseEnumerable(expression!, enumerableType, id, type, out args, out var enumerableExpression))
{
var elementType = enumerableType.GetTypeInfo().GetGenericTypeArguments()[0];
if (TryParseEnumerable(expression!, elementType, id, errorPos, type, out args, out var enumerableExpression))
{
return enumerableExpression;
}
return enumerableExpression;
}

// If args is not set by TryParseEnumerable (in case when the expression is not an Enumerable), do parse the argument list here.
Expand Down Expand Up @@ -2061,8 +2059,10 @@
return ParseMemberAccess(type, null, identifier);
}

private bool TryParseEnumerable(Expression instance, Type elementType, string methodName, int errorPos, Type? type, out Expression[]? args, [NotNullWhen(true)] out Expression? expression)
private bool TryParseEnumerable(Expression instance, Type enumerableType, string methodName, Type? type, out Expression[]? args, [NotNullWhen(true)] out Expression? expression)
{
var elementType = enumerableType.GetTypeInfo().GetGenericTypeArguments()[0];

// Keep the current _parent.
var oldParent = _parent;

Expand Down Expand Up @@ -2124,7 +2124,7 @@
// #633 - For Average without any arguments, try to find the non-generic Average method on the callType for the supplied parameter type.
if (methodName == nameof(Enumerable.Average) && args.Length == 0 && _methodFinder.TryFindAverageMethod(callType, theType, out var averageMethod))
{
expression = Expression.Call(null, averageMethod, new[] { instance });
expression = Expression.Call(null, averageMethod, instance);
return true;
}

Expand All @@ -2136,56 +2136,56 @@
throw ParseError(_textParser.CurrentToken.Pos, Res.FunctionRequiresOneArg, methodName);
}

typeArgs = new[] { ResolveTypeFromArgumentExpression(methodName, args[0]) };
args = new Expression[0];
typeArgs = [ResolveTypeFromArgumentExpression(methodName, args[0])];
args = [];
}
else if (new[] { "Max", "Min", "Select", "OrderBy", "OrderByDescending", "ThenBy", "ThenByDescending", "GroupBy" }.Contains(methodName))
{
if (args.Length == 2)
{
typeArgs = new[] { elementType, args[0].Type, args[1].Type };
typeArgs = [elementType, args[0].Type, args[1].Type];
}
else if (args.Length == 1)
{
typeArgs = new[] { elementType, args[0].Type };
typeArgs = [elementType, args[0].Type];
}
else
{
typeArgs = new[] { elementType };
typeArgs = [elementType];
}
}
else if (methodName == "SelectMany")
{
var bodyType = Expression.Lambda(args[0], innerIt).Body.Type;
var interfaces = bodyType.GetInterfaces().Union(new[] { bodyType });
Type interfaceType = interfaces.Single(i => i.Name == typeof(IEnumerable<>).Name);
Type resultType = interfaceType.GetTypeInfo().GetGenericTypeArguments()[0];
typeArgs = new[] { elementType, resultType };
var interfaces = bodyType.GetInterfaces().Union([bodyType]);
var interfaceType = interfaces.Single(i => i.Name == typeof(IEnumerable<>).Name);
var resultType = interfaceType.GetTypeInfo().GetGenericTypeArguments()[0];
typeArgs = [elementType, resultType];
}
else
{
typeArgs = new[] { elementType };
typeArgs = [elementType];
}

if (args.Length == 0)
{
args = new[] { instance };
args = [instance];
}
else
{
if (new[] { "Concat", "Contains", "ContainsKey", "DefaultIfEmpty", "Except", "Intersect", "Skip", "Take", "Union" }.Contains(methodName))
if (new[] { "Concat", "Contains", "ContainsKey", "DefaultIfEmpty", "Except", "Intersect", "Skip", "Take", "Union", "SequenceEqual" }.Contains(methodName))
{
args = new[] { instance, args[0] };
args = [instance, args[0]];
}
else
{
if (args.Length == 2)
{
args = new[] { instance, Expression.Lambda(args[0], innerIt), Expression.Lambda(args[1], innerIt) };
args = [instance, Expression.Lambda(args[0], innerIt), Expression.Lambda(args[1], innerIt)];
}
else
{
args = new[] { instance, Expression.Lambda(args[0], innerIt) };
args = [instance, Expression.Lambda(args[0], innerIt)];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Linq.Dynamic.Core.Parser;
using System.Linq.Expressions;
using Xunit;

namespace System.Linq.Dynamic.Core.Tests.Parser;

partial class ExpressionParserTests
{
[Fact]
public void Parse_SequenceEqual()
{
// Arrange
var parameter = Expression.Parameter(typeof(Entity), "Entity");

var parser = new ExpressionParser(
[parameter],
"Entity.ArrayA.SequenceEqual(Entity.ArrayB)",
null,
null);

// Act
parser.Parse(typeof(bool));
}

public class Entity
{
public string[] ArrayA { get; set; } = [];

public string[] ArrayB { get; set; } = [];
}
}
Loading