Fixing the expression convertor for imbricked lambdas

This commit is contained in:
Zoe Roux 2020-09-12 00:49:28 +02:00
parent fb14eb4ef2
commit 75c9d703f7
2 changed files with 7 additions and 13 deletions

View File

@ -342,26 +342,18 @@ namespace Kyoo
} }
internal Expression<TTo> VisitAndConvert() internal Expression<TTo> VisitAndConvert()
{
return (Expression<TTo>)RunGenericMethod(
this,
"VisitLambda",
_expression.GetType().GetGenericArguments().First(),
_expression);
}
protected override Expression VisitLambda<T>(Expression<T> node)
{ {
Type returnType = _expression.Type.GetGenericArguments().Last(); Type returnType = _expression.Type.GetGenericArguments().Last();
Expression body = node.ReturnType == returnType Expression body = _expression.ReturnType == returnType
? Visit(node.Body) ? Visit(_expression.Body)
: Expression.Convert(Visit(node.Body)!, returnType); : Expression.Convert(Visit(_expression.Body)!, returnType);
return Expression.Lambda<TTo>(body!, _newParams); return Expression.Lambda<TTo>(body!, _newParams);
} }
protected override Expression VisitParameter(ParameterExpression node) protected override Expression VisitParameter(ParameterExpression node)
{ {
return _newParams.First(x => x.Name == node.Name); Console.WriteLine($"Rewritting parameter: {node.Name}");
return _newParams.FirstOrDefault(x => x.Name == node.Name) ?? node;
} }
} }
} }

View File

@ -23,6 +23,8 @@ namespace Kyoo.CommonApi
public static Expression<Func<T, bool>> ParseWhere<T>(Dictionary<string, string> where, public static Expression<Func<T, bool>> ParseWhere<T>(Dictionary<string, string> where,
Expression<Func<T, bool>> defaultWhere = null) Expression<Func<T, bool>> defaultWhere = null)
{ {
return defaultWhere;
if (where == null || where.Count == 0) if (where == null || where.Count == 0)
return defaultWhere; return defaultWhere;