Skip to content

Fix parameter detection for expression arguments #2542

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

Merged
merged 2 commits into from
Sep 17, 2020
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
25 changes: 25 additions & 0 deletions src/NHibernate.Test/Async/Linq/WhereSubqueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -707,5 +707,30 @@ public async Task NullComparedToMemberInitExpressionInWhereClauseAsync()

Assert.That(result.Count, Is.EqualTo(45));
}

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 async Task ImplicitConversionInsideWhereSubqueryExpressionAsync()
{
if (!Dialect.SupportsScalarSubSelects)
Assert.Ignore(Dialect.GetType().Name + " does not support scalar sub-queries");

var spec = new Specification<Order>(x => x.Freight > 1000);
await (db.Orders.Where(o => db.Orders.Where(spec).Any(x => x.OrderId == o.OrderId)).ToListAsync());
}
}
}
25 changes: 25 additions & 0 deletions src/NHibernate.Test/Linq/WhereSubqueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -788,5 +788,30 @@ public void NullComparedToMemberInitExpressionInWhereClause()

Assert.That(result.Count, Is.EqualTo(45));
}

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()
{
if (!Dialect.SupportsScalarSubSelects)
Assert.Ignore(Dialect.GetType().Name + " does not support scalar sub-queries");

var spec = new Specification<Order>(x => x.Freight > 1000);
db.Orders.Where(o => db.Orders.Where(spec).Any(x => x.OrderId == o.OrderId)).ToList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ private Expression EvaluateSubtree(Expression subtree)

private bool ContainsVariable(Expression expression)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maca88 can this method be moved to NhEvaluatableExpressionFilter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not, it was placed here due to #2458, to evaluate expression like string.Fromat("{0}", variable).

{
if (!(expression is UnaryExpression unaryExpression))
if (!(expression is UnaryExpression unaryExpression) ||
// Avoid detecting expression variables as parameters
typeof(Expression).IsAssignableFrom(expression.Type))
{
return false;
}
Expand Down