Skip to content

Commit 67dcb7e

Browse files
authoredAug 17, 2022
Fix inner join issue when referenced in Where clause in LINQ (nhibernate#3110)
1 parent 1793699 commit 67dcb7e

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed
 

‎src/NHibernate.Test/Async/Linq/ByMethod/JoinTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@ public async Task LeftJoinExtensionMethodWithMultipleKeyPropertiesAsync()
111111
}
112112
}
113113

114+
[Test(Description = "GH-3104")]
115+
public async Task LeftJoinExtensionMethodWithInnerJoinAfterAsync()
116+
{
117+
var animals = await (db.Animals
118+
.LeftJoin(db.Mammals, o => o.Id, i => i.Id, (o, i) => new { animal = o, mammalLeft1 = i })
119+
.LeftJoin(db.Mammals, x => x.mammalLeft1.Id, y => y.Id, (o, i) => new { o.animal, o.mammalLeft1, mammalLeft2 = i })
120+
.Join(db.Mammals, o => o.mammalLeft2.Id, y => y.Id, (o, i) => new { o.animal, o.mammalLeft1, o.mammalLeft2, mammalInner = i })
121+
.Where(x => x.mammalLeft1.SerialNumber.StartsWith("9"))
122+
.Where(x => x.mammalLeft2.SerialNumber.StartsWith("9"))
123+
.Where(x => x.animal.SerialNumber.StartsWith("9"))
124+
.Where(x => x.mammalInner.SerialNumber.StartsWith("9"))
125+
.Select(x => new { SerialNumber = x.animal.SerialNumber })
126+
.ToListAsync());
127+
128+
Assert.That(animals.Count, Is.EqualTo(1));
129+
}
130+
114131
[Test]
115132
public async Task LeftJoinExtensionMethodWithOuterReferenceInWhereClauseOnlyAsync()
116133
{

‎src/NHibernate.Test/Linq/ByMethod/JoinTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ public void LeftJoinExtensionMethodWithMultipleKeyProperties()
9999
}
100100
}
101101

102+
[Test(Description = "GH-3104")]
103+
public void LeftJoinExtensionMethodWithInnerJoinAfter()
104+
{
105+
var animals = db.Animals
106+
.LeftJoin(db.Mammals, o => o.Id, i => i.Id, (o, i) => new { animal = o, mammalLeft1 = i })
107+
.LeftJoin(db.Mammals, x => x.mammalLeft1.Id, y => y.Id, (o, i) => new { o.animal, o.mammalLeft1, mammalLeft2 = i })
108+
.Join(db.Mammals, o => o.mammalLeft2.Id, y => y.Id, (o, i) => new { o.animal, o.mammalLeft1, o.mammalLeft2, mammalInner = i })
109+
.Where(x => x.mammalLeft1.SerialNumber.StartsWith("9"))
110+
.Where(x => x.mammalLeft2.SerialNumber.StartsWith("9"))
111+
.Where(x => x.animal.SerialNumber.StartsWith("9"))
112+
.Where(x => x.mammalInner.SerialNumber.StartsWith("9"))
113+
.Select(x => new { SerialNumber = x.animal.SerialNumber })
114+
.ToList();
115+
116+
Assert.That(animals.Count, Is.EqualTo(1));
117+
}
118+
102119
[Test]
103120
public void LeftJoinExtensionMethodWithOuterReferenceInWhereClauseOnly()
104121
{

‎src/NHibernate/Linq/GroupJoin/GroupJoinAggregateDetectionVisitor.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Collections.ObjectModel;
43
using System.Linq.Expressions;
54
using NHibernate.Linq.Expressions;
65
using NHibernate.Linq.Visitors;
7-
using Remotion.Linq;
86
using Remotion.Linq.Clauses;
97
using Remotion.Linq.Clauses.Expressions;
108

@@ -63,13 +61,13 @@ protected override Expression VisitMember(MemberExpression expression)
6361

6462
protected override Expression VisitQuerySourceReference(QuerySourceReferenceExpression expression)
6563
{
66-
var fromClause = (FromClauseBase) expression.ReferencedQuerySource;
67-
68-
var querySourceReference = fromClause.FromExpression as QuerySourceReferenceExpression;
69-
if (querySourceReference != null)
64+
if (!(expression.ReferencedQuerySource is FromClauseBase fromClause))
65+
{
66+
}
67+
else if (fromClause.FromExpression is QuerySourceReferenceExpression querySourceReference)
7068
{
71-
var groupJoinClause = querySourceReference.ReferencedQuerySource as GroupJoinClause;
72-
if (groupJoinClause != null && _groupJoinClauses.Contains(groupJoinClause))
69+
if (querySourceReference.ReferencedQuerySource is GroupJoinClause groupJoinClause &&
70+
_groupJoinClauses.Contains(groupJoinClause))
7371
{
7472
if (_inAggregate.FlagIsFalse)
7573
{

0 commit comments

Comments
 (0)
Please sign in to comment.