Skip to content

Fix using a loaded IPersistentCollection in a Query #1560

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
Jan 29, 2018
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
12 changes: 12 additions & 0 deletions src/NHibernate.Test/Async/Linq/WhereTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,18 @@ public async Task SelectOnCollectionReturnsResultAsync()
Assert.That(result.Children, Is.Not.Empty);
}

[Test(Description = "GH-1556")]
public async Task ContainsOnPersistedCollectionAsync()
{
var animal = await (session.Query<Animal>().SingleAsync(a => a.SerialNumber == "123"));

var result = await (session.Query<Animal>()
.Where(e => animal.Children.Contains(e.Father))
.OrderBy(e => e.Id)
.FirstOrDefaultAsync());
Assert.That(result, Is.Not.Null);
Assert.That(result.SerialNumber, Is.EqualTo("1121"));
}

private static List<object[]> CanUseCompareInQueryDataSource()
{
Expand Down
12 changes: 12 additions & 0 deletions src/NHibernate.Test/Linq/WhereTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,18 @@ public void SelectOnCollectionReturnsResult()
Assert.That(result.Children, Is.Not.Empty);
}

[Test(Description = "GH-1556")]
public void ContainsOnPersistedCollection()
{
var animal = session.Query<Animal>().Single(a => a.SerialNumber == "123");

var result = session.Query<Animal>()
.Where(e => animal.Children.Contains(e.Father))
.OrderBy(e => e.Id)
.FirstOrDefault();
Assert.That(result, Is.Not.Null);
Assert.That(result.SerialNumber, Is.EqualTo("1121"));
}

private static List<object[]> CanUseCompareInQueryDataSource()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using NHibernate.Collection;
using Remotion.Linq.Clauses.Expressions;
using Remotion.Linq.Parsing;
using Remotion.Linq.Parsing.ExpressionVisitors;
Expand All @@ -12,13 +13,12 @@ internal class NhPartialEvaluatingExpressionVisitor : RelinqExpressionVisitor, I
{
protected override Expression VisitConstant(ConstantExpression expression)
{
var value = expression.Value as Expression;
if (value == null)
if (expression.Value is Expression value)
{
return base.VisitConstant(expression);
return EvaluateIndependentSubtrees(value);
}

return EvaluateIndependentSubtrees(value);
return base.VisitConstant(expression);
}

public static Expression EvaluateIndependentSubtrees(Expression expression)
Expand All @@ -37,6 +37,16 @@ public Expression VisitPartialEvaluationException(PartialEvaluationExceptionExpr

internal class NhEvaluatableExpressionFilter : EvaluatableExpressionFilterBase
{
public override bool IsEvaluatableConstant(ConstantExpression node)
{
if (node.Value is IPersistentCollection && node.Value is IQueryable)
{
return false;
}

return base.IsEvaluatableConstant(node);
}

public override bool IsEvaluatableMethodCall(MethodCallExpression node)
{
if (node == null)
Expand Down