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 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
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>
21 changes: 21 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,27 @@ public async Task ContainsOnPersistedCollectionAsync()
Assert.That(result.SerialNumber, Is.EqualTo("1121"));
}

[Test]
public async Task CanCompareAggregateResultAsync()
{
if (!Dialect.SupportsScalarSubSelects)
{
Assert.Ignore(Dialect.GetType().Name + " does not support scalar sub-queries");
}

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
21 changes: 21 additions & 0 deletions src/NHibernate.Test/Linq/WhereTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,27 @@ public void ContainsOnPersistedCollection()
Assert.That(result.SerialNumber, Is.EqualTo("1121"));
}

[Test]
public void CanCompareAggregateResult()
{
if (!Dialect.SupportsScalarSubSelects)
{
Assert.Ignore(Dialect.GetType().Name + " does not support scalar sub-queries");
}

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[expression.Left] = leftType;
_notCastableExpressions[expression.Right] = rightType;
}

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