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

Fix DynamicExpressionParser for IQueryable #802

Merged
merged 1 commit into from
Apr 23, 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
16 changes: 6 additions & 10 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,16 +1799,12 @@ private Expression ParseMemberAccess(Type? type, Expression? expression, string?

var isStaticAccess = expression == null;

if (!isStaticAccess)
if (!isStaticAccess && TypeHelper.TryFindGenericType(typeof(IEnumerable<>), type, out var enumerableType))
{
var enumerableType = TypeHelper.FindGenericType(typeof(IEnumerable<>), type);
if (enumerableType != null)
var elementType = enumerableType.GetTypeInfo().GetGenericTypeArguments()[0];
if (TryParseEnumerable(expression!, elementType, id, errorPos, 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;
}
}

Expand Down Expand Up @@ -2075,7 +2071,7 @@ private bool TryParseEnumerable(Expression instance, Type elementType, string me
expression = null;
return false;
}

if (type != null && TypeHelper.IsDictionary(type) && _methodFinder.ContainsMethod(type, methodName, false))
{
var dictionaryMethod = type.GetMethod(methodName)!;
Expand All @@ -2087,7 +2083,7 @@ private bool TryParseEnumerable(Expression instance, Type elementType, string me
_methodFinder.CheckAggregateMethodAndTryUpdateArgsToMatchMethodArgs(methodName, ref args);

var callType = typeof(Enumerable);
if (type != null && TypeHelper.FindGenericType(typeof(IQueryable<>), type) != null && _methodFinder.ContainsMethod(type, methodName))
if (TypeHelper.TryFindGenericType(typeof(IQueryable<>), type, out _) && _methodFinder.ContainsMethod(typeof(Queryable), methodName))
{
callType = typeof(Queryable);
}
Expand Down
19 changes: 10 additions & 9 deletions src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,32 @@ public static bool TryGetFirstGenericArgument(Type type, [NotNullWhen(true)] out
return true;
}

public static Type? FindGenericType(Type generic, Type? type)
public static bool TryFindGenericType(Type generic, Type? type, [NotNullWhen(true)] out Type? foundType)
{
while (type != null && type != typeof(object))
{
if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == generic)
{
return type;
foundType = type;
return true;
}

if (generic.GetTypeInfo().IsInterface)
{
foreach (Type interfaceType in type.GetInterfaces())
foreach (var interfaceType in type.GetInterfaces())
{
var foundType = FindGenericType(generic, interfaceType);
if (foundType != null)
if (TryFindGenericType(generic, interfaceType, out foundType))
{
return foundType;
return true;
}
}
}

type = type.GetTypeInfo().BaseType;
}

return null;
foundType = null;
return false;
}

public static bool IsCompatibleWith(Type source, Type target)
Expand Down Expand Up @@ -509,12 +510,12 @@ public static bool TryParseEnum(string value, Type? type, [NotNullWhen(true)] ou
public static bool IsDictionary(Type? type)
{
return
FindGenericType(typeof(IDictionary<,>), type) != null ||
TryFindGenericType(typeof(IDictionary<,>), type, out _) ||
#if NET35 || NET40
// ReSharper disable once RedundantLogicalConditionalExpressionOperand
false;
#else
FindGenericType(typeof(IReadOnlyDictionary<,>), type) != null;
TryFindGenericType(typeof(IReadOnlyDictionary<,>), type, out _);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Tests.Helpers;
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
using System.Linq.Dynamic.Core.Tests.TestHelpers;
using System.Linq.Expressions;
Expand Down Expand Up @@ -577,6 +578,27 @@ public void DynamicExpressionParser_ParseLambda_Select_2()
Assert.NotNull(result);
}

// #801
[Fact]
public void DynamicExpressionParser_ParseLambda_IQueryable()
{
// Assign
var qry = new[]
{
new
{
MessageClassName = "mc"
}
}.AsQueryable();

// Act
var expression = DynamicExpressionParser.ParseLambda(qry.GetType(), null, "it.GroupBy(MessageClassName).Select(it.Key).Where(it != null).Distinct().OrderBy(it).Take(1000)");

// Assert
expression.ToDebugView().Should().Contain(".Call System.Linq.Queryable");
expression.ToDebugView().Should().NotContain(".Call System.Linq.Enumerable");
}

// https://github.com/StefH/System.Linq.Dynamic.Core/issues/58
[Fact]
public void DynamicExpressionParser_ParseLambda_Issue58()
Expand Down
Loading