Skip to content

Commit a6a3c53

Browse files
gliljashazzik
authored andcommitted
Fix using a loaded IPersistentCollection in a Query (#1560)
1 parent 24b4544 commit a6a3c53

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/NHibernate.Test/Async/Linq/WhereTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,18 @@ public async Task SelectOnCollectionReturnsResultAsync()
844844
Assert.That(result.Children, Is.Not.Empty);
845845
}
846846

847+
[Test(Description = "GH-1556")]
848+
public async Task ContainsOnPersistedCollectionAsync()
849+
{
850+
var animal = await (session.Query<Animal>().SingleAsync(a => a.SerialNumber == "123"));
851+
852+
var result = await (session.Query<Animal>()
853+
.Where(e => animal.Children.Contains(e.Father))
854+
.OrderBy(e => e.Id)
855+
.FirstOrDefaultAsync());
856+
Assert.That(result, Is.Not.Null);
857+
Assert.That(result.SerialNumber, Is.EqualTo("1121"));
858+
}
847859

848860
private static List<object[]> CanUseCompareInQueryDataSource()
849861
{

src/NHibernate.Test/Linq/WhereTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,18 @@ public void SelectOnCollectionReturnsResult()
812812
Assert.That(result.Children, Is.Not.Empty);
813813
}
814814

815+
[Test(Description = "GH-1556")]
816+
public void ContainsOnPersistedCollection()
817+
{
818+
var animal = session.Query<Animal>().Single(a => a.SerialNumber == "123");
819+
820+
var result = session.Query<Animal>()
821+
.Where(e => animal.Children.Contains(e.Father))
822+
.OrderBy(e => e.Id)
823+
.FirstOrDefault();
824+
Assert.That(result, Is.Not.Null);
825+
Assert.That(result.SerialNumber, Is.EqualTo("1121"));
826+
}
815827

816828
private static List<object[]> CanUseCompareInQueryDataSource()
817829
{

src/NHibernate/Linq/Visitors/NhPartialEvaluatingExpressionVisitor.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Linq.Expressions;
4+
using NHibernate.Collection;
45
using Remotion.Linq.Clauses.Expressions;
56
using Remotion.Linq.Parsing;
67
using Remotion.Linq.Parsing.ExpressionVisitors;
@@ -12,13 +13,12 @@ internal class NhPartialEvaluatingExpressionVisitor : RelinqExpressionVisitor, I
1213
{
1314
protected override Expression VisitConstant(ConstantExpression expression)
1415
{
15-
var value = expression.Value as Expression;
16-
if (value == null)
16+
if (expression.Value is Expression value)
1717
{
18-
return base.VisitConstant(expression);
18+
return EvaluateIndependentSubtrees(value);
1919
}
2020

21-
return EvaluateIndependentSubtrees(value);
21+
return base.VisitConstant(expression);
2222
}
2323

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

3838
internal class NhEvaluatableExpressionFilter : EvaluatableExpressionFilterBase
3939
{
40+
public override bool IsEvaluatableConstant(ConstantExpression node)
41+
{
42+
if (node.Value is IPersistentCollection && node.Value is IQueryable)
43+
{
44+
return false;
45+
}
46+
47+
return base.IsEvaluatableConstant(node);
48+
}
49+
4050
public override bool IsEvaluatableMethodCall(MethodCallExpression node)
4151
{
4252
if (node == null)

0 commit comments

Comments
 (0)