Skip to content

Fix inner join issue when referenced in Where clause in LINQ #3110

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 5 commits into from
Aug 17, 2022
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
17 changes: 17 additions & 0 deletions src/NHibernate.Test/Async/Linq/ByMethod/JoinTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ public async Task LeftJoinExtensionMethodWithMultipleKeyPropertiesAsync()
}
}

[Test(Description = "GH-3104")]
public async Task LeftJoinExtensionMethodWithInnerJoinAfterAsync()
{
var animals = await (db.Animals
.LeftJoin(db.Mammals, o => o.Id, i => i.Id, (o, i) => new { animal = o, mammalLeft1 = i })
.LeftJoin(db.Mammals, x => x.mammalLeft1.Id, y => y.Id, (o, i) => new { o.animal, o.mammalLeft1, mammalLeft2 = i })
.Join(db.Mammals, o => o.mammalLeft2.Id, y => y.Id, (o, i) => new { o.animal, o.mammalLeft1, o.mammalLeft2, mammalInner = i })
.Where(x => x.mammalLeft1.SerialNumber.StartsWith("9"))
.Where(x => x.mammalLeft2.SerialNumber.StartsWith("9"))
.Where(x => x.animal.SerialNumber.StartsWith("9"))
.Where(x => x.mammalInner.SerialNumber.StartsWith("9"))
.Select(x => new { SerialNumber = x.animal.SerialNumber })
.ToListAsync());

Assert.That(animals.Count, Is.EqualTo(1));
}

[Test]
public async Task LeftJoinExtensionMethodWithOuterReferenceInWhereClauseOnlyAsync()
{
Expand Down
17 changes: 17 additions & 0 deletions src/NHibernate.Test/Linq/ByMethod/JoinTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ public void LeftJoinExtensionMethodWithMultipleKeyProperties()
}
}

[Test(Description = "GH-3104")]
public void LeftJoinExtensionMethodWithInnerJoinAfter()
{
var animals = db.Animals
.LeftJoin(db.Mammals, o => o.Id, i => i.Id, (o, i) => new { animal = o, mammalLeft1 = i })
.LeftJoin(db.Mammals, x => x.mammalLeft1.Id, y => y.Id, (o, i) => new { o.animal, o.mammalLeft1, mammalLeft2 = i })
.Join(db.Mammals, o => o.mammalLeft2.Id, y => y.Id, (o, i) => new { o.animal, o.mammalLeft1, o.mammalLeft2, mammalInner = i })
.Where(x => x.mammalLeft1.SerialNumber.StartsWith("9"))
.Where(x => x.mammalLeft2.SerialNumber.StartsWith("9"))
.Where(x => x.animal.SerialNumber.StartsWith("9"))
.Where(x => x.mammalInner.SerialNumber.StartsWith("9"))
.Select(x => new { SerialNumber = x.animal.SerialNumber })
.ToList();

Assert.That(animals.Count, Is.EqualTo(1));
}

[Test]
public void LeftJoinExtensionMethodWithOuterReferenceInWhereClauseOnly()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using NHibernate.Linq.Expressions;
using NHibernate.Linq.Visitors;
using Remotion.Linq;
using Remotion.Linq.Clauses;
using Remotion.Linq.Clauses.Expressions;

Expand Down Expand Up @@ -63,13 +61,13 @@ protected override Expression VisitMember(MemberExpression expression)

protected override Expression VisitQuerySourceReference(QuerySourceReferenceExpression expression)
{
var fromClause = (FromClauseBase) expression.ReferencedQuerySource;

var querySourceReference = fromClause.FromExpression as QuerySourceReferenceExpression;
if (querySourceReference != null)
if (!(expression.ReferencedQuerySource is FromClauseBase fromClause))
{
}
else if (fromClause.FromExpression is QuerySourceReferenceExpression querySourceReference)
{
var groupJoinClause = querySourceReference.ReferencedQuerySource as GroupJoinClause;
if (groupJoinClause != null && _groupJoinClauses.Contains(groupJoinClause))
if (querySourceReference.ReferencedQuerySource is GroupJoinClause groupJoinClause &&
_groupJoinClauses.Contains(groupJoinClause))
{
if (_inAggregate.FlagIsFalse)
{
Expand Down