Skip to content

Fix subquery "Item with Same Key has already been added” exception for Linq provider #2664

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 4 commits into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/NHibernate.DomainModel/Northwind/Entities/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public virtual DateTime? OrderDate
set { _orderDate = value; }
}

public virtual DateTime RequiredOrderDate { get; set; }

public virtual DateTime? RequiredDate
{
get { return _requiredDate; }
Expand Down Expand Up @@ -106,4 +108,4 @@ public virtual void RemoveOrderLine(OrderLine orderLine)
}
}
}
}
}
5 changes: 4 additions & 1 deletion src/NHibernate.DomainModel/Northwind/Mappings/Order.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<property name="OrderDate" column="OrderDate" type="DateTime"
access="field.camelcase-underscore"/>


<property name="RequiredOrderDate" formula="OrderDate" insert="false" update="false" />

<property name="RequiredDate" column="RequiredDate" type="DateTime"
access="field.camelcase-underscore"/>

Expand Down Expand Up @@ -60,4 +63,4 @@

</class>

</hibernate-mapping>
</hibernate-mapping>
16 changes: 16 additions & 0 deletions src/NHibernate.Test/Async/Linq/WhereTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,22 @@ public async Task ContainsOnPersistedCollectionAsync()
Assert.That(result.SerialNumber, Is.EqualTo("1121"));
}

[Test]
public async Task CanCompareAggregateResultAsync()
{
await (session.Query<Customer>()
.Select(o => new AggregateDate { Id = o.CustomerId, MaxDate = o.Orders.Max(l => l.RequiredOrderDate)})
.Where(o => o.MaxDate <= DateTime.Today && o.MaxDate >= DateTime.Today)
.ToListAsync());
}

private class AggregateDate
{
public string Id { get; set; }

public DateTime? MaxDate { get; set; }
}

private static List<object[]> CanUseCompareInQueryDataSource()
{
return new List<object[]>
Expand Down
16 changes: 16 additions & 0 deletions src/NHibernate.Test/Linq/WhereTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,22 @@ public void ContainsOnPersistedCollection()
Assert.That(result.SerialNumber, Is.EqualTo("1121"));
}

[Test]
public void CanCompareAggregateResult()
{
session.Query<Customer>()
.Select(o => new AggregateDate { Id = o.CustomerId, MaxDate = o.Orders.Max(l => l.RequiredOrderDate)})
.Where(o => o.MaxDate <= DateTime.Today && o.MaxDate >= DateTime.Today)
.ToList();
}

private class AggregateDate
{
public string Id { get; set; }

public DateTime? MaxDate { get; set; }
}

private static List<object[]> CanUseCompareInQueryDataSource()
{
return new List<object[]>
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Linq/Visitors/HqlGeneratorExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ protected HqlTreeNode VisitBinaryExpression(BinaryExpression expression)
var rightType = GetExpressionType(expression.Right);
if (leftType != null && leftType == rightType)
{
_notCastableExpressions.Add(expression.Left, leftType);
_notCastableExpressions.Add(expression.Right, rightType);
_notCastableExpressions.TryAdd(expression.Left, leftType);
Copy link
Member

Choose a reason for hiding this comment

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

TryAdd is not available in .net standard 2.0 and .net 4.6.1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an extension method for them.

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 we replace Add with Set?

_notCastableExpressions[expression.Right] = rightType;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Somehow I overlooked the comment. Yes we can, replaced with Set.
Also now I realized that I created the branch to this repository instead on my fork. We should remove the branch after merging.

Copy link
Member

Choose a reason for hiding this comment

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

Somehow I overlooked the comment.

This is what I thought.

_notCastableExpressions.TryAdd(expression.Right, rightType);
}

if (expression.NodeType == ExpressionType.Equal)
Expand Down