-
Notifications
You must be signed in to change notification settings - Fork 934
Unable to cast "System.Linq.Expressions.UnaryExpression" to "System.Linq.Expressions.LambdaExpression". #2537
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
Comments
This comment has been minimized.
This comment has been minimized.
Yes it's reproducible on 5.3.3 and has nothing to do with custom provider. It's something related to implicit conversion to expression. public class Specification<T>
{
private Expression<Func<T, bool>> _expression;
public Specification(Expression<Func<T, bool>> expression)
{
_expression = expression;
}
public static implicit operator Expression<Func<T, bool>>(Specification<T> specification)
{
return specification._expression;
}
}
[Test]
public void ImplicitConversionInsideWhereSubqueryExpression()
{
var spec = new Specification<Order>(x => x.Freight > 1000);
db.Orders.Where(o => db.Orders.Where(spec).Any(x => x.OrderId == o.OrderId)).ToList();
} |
As a workaround you can try to add the following code to protected override Expression VisitUnary(UnaryExpression node)
{
if (node.NodeType == ExpressionType.Convert && typeof(BaseSpecification).IsAssignableFrom(node.Operand.Type))
{
return ((BaseSpecification)FindValue(node.Operand)).ToExpression();
}
return base.VisitUnary(node);
}
private static object FindValue(Expression expression)
{
if (expression.NodeType == ExpressionType.MemberAccess)
{
var memberAccess = (MemberExpression) expression;
if (memberAccess.Expression == null || memberAccess.Expression.NodeType == ExpressionType.Constant)
{
var constantValue = ((ConstantExpression) memberAccess.Expression)?.Value;
var member = memberAccess.Member;
switch (member.MemberType)
{
case MemberTypes.Field:
return ((FieldInfo) member).GetValue(constantValue);
case MemberTypes.Property:
return ((PropertyInfo) member).GetValue(constantValue);
}
}
}
throw new NotSupportedException();
} Also in 5.3 you can drop all protected override IQueryProvider CreateWithOptions(NhQueryableOptions options)
{
return new CustomQueryProvider(Session, Collection, options);
}
private CustomQueryProvider(ISessionImplementor session, object collection, NhQueryableOptions options): base(session, collection, options)
{
} |
What is the type of unary expression? Is it |
I suspect it's Convert |
Hello.
After update from 5.2.7 to 5.3.* we have some issue with custom QueryProvider. I make test application -
https://github.com/emejibka/nhibernate-specification-subquery-bug
The text was updated successfully, but these errors were encountered: