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

Handle UseParameterizedNamesInDynamicQuery when parsing dynamic OfType function #723

Merged
merged 3 commits into from
Jul 15, 2023
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
31 changes: 22 additions & 9 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2006,23 +2006,36 @@ private Expression ParseEnumerable(Expression instance, Type elementType, string
private Type ResolveTypeFromArgumentExpression(string functionName, Expression argumentExpression, int? arguments = null)
{
string argument = arguments == null ? string.Empty : arguments == 1 ? "first " : "second ";

switch (argumentExpression)
{
case ConstantExpression constantExpression:
switch (constantExpression.Value)
return ResolveTypeFromExpressionValue(functionName, constantExpression, argument);

case MemberExpression memberExpression:
if (_expressionHelper.TryUnwrapAsConstantExpression(memberExpression, out var unwrappedConstantExpression))
{
case string typeName:
return ResolveTypeStringFromArgument(typeName);
return ResolveTypeFromExpressionValue(functionName, unwrappedConstantExpression, argument);
}

case Type type:
return type;
break;
}

default:
throw ParseError(_textParser.CurrentToken.Pos, Res.FunctionRequiresNotNullArgOfType, functionName, argument, "string or System.Type");
}
throw ParseError(_textParser.CurrentToken.Pos, Res.FunctionRequiresNotNullArgOfType, functionName, argument, "ConstantExpression");
}

private Type ResolveTypeFromExpressionValue(string functionName, ConstantExpression constantExpression, string argument)
{
switch (constantExpression.Value)
{
case string typeName:
return ResolveTypeStringFromArgument(typeName);

case Type type:
return type;

default:
throw ParseError(_textParser.CurrentToken.Pos, Res.FunctionRequiresNotNullArgOfType, functionName, argument, "ConstantExpression");
throw ParseError(_textParser.CurrentToken.Pos, Res.FunctionRequiresNotNullArgOfType, functionName, argument, "string or System.Type");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ public void OfType_Dynamic_WithFullName()
Check.That(oftypeDynamic.Length).Equals(oftype.Length);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void OfType_Dynamic_WithFullName_UseParameterizedNamesInDynamicQuery(bool useParameterizedNamesInDynamicQuery)
{
// Assign
var qry = new[]
{
new CompanyWithBaseEmployees
{
Employees = new BaseEmployee[]
{
new Worker { Name = "e" }, new Boss { Name = "e" }
}
}
}.AsQueryable();

var parsingConfig = new ParsingConfig
{
UseParameterizedNamesInDynamicQuery = useParameterizedNamesInDynamicQuery
};

// Act
var oftype = qry.Select(c => c.Employees.OfType<Worker>().Where(e => e.Name == "e")).ToArray();
var oftypeDynamic = qry.Select(parsingConfig, "Employees.OfType(\"System.Linq.Dynamic.Core.Tests.Entities.Worker\").Where(Name == \"e\")").ToDynamicArray();

// Assert
Check.That(oftypeDynamic.Length).Equals(oftype.Length);
}

internal class Base { }

internal class DerivedA : Base { }
Expand Down