Skip to content

Commit c058080

Browse files
authoredFeb 14, 2021
Fix subquery "Item with Same Key has already been added” exception for Linq provider (nhibernate#2664)
Fixes nhibernate#2659
1 parent 21701a0 commit c058080

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed
 

‎src/NHibernate.DomainModel/Northwind/Entities/Order.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public virtual DateTime? OrderDate
4747
set { _orderDate = value; }
4848
}
4949

50+
public virtual DateTime RequiredOrderDate { get; set; }
51+
5052
public virtual DateTime? RequiredDate
5153
{
5254
get { return _requiredDate; }
@@ -106,4 +108,4 @@ public virtual void RemoveOrderLine(OrderLine orderLine)
106108
}
107109
}
108110
}
109-
}
111+
}

‎src/NHibernate.DomainModel/Northwind/Mappings/Order.hbm.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<property name="OrderDate" column="OrderDate" type="DateTime"
1818
access="field.camelcase-underscore"/>
1919

20+
21+
<property name="RequiredOrderDate" formula="OrderDate" insert="false" update="false" />
22+
2023
<property name="RequiredDate" column="RequiredDate" type="DateTime"
2124
access="field.camelcase-underscore"/>
2225

@@ -60,4 +63,4 @@
6063

6164
</class>
6265

63-
</hibernate-mapping>
66+
</hibernate-mapping>

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

+21
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,27 @@ public async Task ContainsOnPersistedCollectionAsync()
848848
Assert.That(result.SerialNumber, Is.EqualTo("1121"));
849849
}
850850

851+
[Test]
852+
public async Task CanCompareAggregateResultAsync()
853+
{
854+
if (!Dialect.SupportsScalarSubSelects)
855+
{
856+
Assert.Ignore(Dialect.GetType().Name + " does not support scalar sub-queries");
857+
}
858+
859+
await (session.Query<Customer>()
860+
.Select(o => new AggregateDate { Id = o.CustomerId, MaxDate = o.Orders.Max(l => l.RequiredOrderDate)})
861+
.Where(o => o.MaxDate <= DateTime.Today && o.MaxDate >= DateTime.Today)
862+
.ToListAsync());
863+
}
864+
865+
private class AggregateDate
866+
{
867+
public string Id { get; set; }
868+
869+
public DateTime? MaxDate { get; set; }
870+
}
871+
851872
private static List<object[]> CanUseCompareInQueryDataSource()
852873
{
853874
return new List<object[]>

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

+21
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,27 @@ public void ContainsOnPersistedCollection()
849849
Assert.That(result.SerialNumber, Is.EqualTo("1121"));
850850
}
851851

852+
[Test]
853+
public void CanCompareAggregateResult()
854+
{
855+
if (!Dialect.SupportsScalarSubSelects)
856+
{
857+
Assert.Ignore(Dialect.GetType().Name + " does not support scalar sub-queries");
858+
}
859+
860+
session.Query<Customer>()
861+
.Select(o => new AggregateDate { Id = o.CustomerId, MaxDate = o.Orders.Max(l => l.RequiredOrderDate)})
862+
.Where(o => o.MaxDate <= DateTime.Today && o.MaxDate >= DateTime.Today)
863+
.ToList();
864+
}
865+
866+
private class AggregateDate
867+
{
868+
public string Id { get; set; }
869+
870+
public DateTime? MaxDate { get; set; }
871+
}
872+
852873
private static List<object[]> CanUseCompareInQueryDataSource()
853874
{
854875
return new List<object[]>

‎src/NHibernate/Linq/Visitors/HqlGeneratorExpressionVisitor.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ protected HqlTreeNode VisitBinaryExpression(BinaryExpression expression)
317317
var rightType = GetExpressionType(expression.Right);
318318
if (leftType != null && leftType == rightType)
319319
{
320-
_notCastableExpressions.Add(expression.Left, leftType);
321-
_notCastableExpressions.Add(expression.Right, rightType);
320+
_notCastableExpressions[expression.Left] = leftType;
321+
_notCastableExpressions[expression.Right] = rightType;
322322
}
323323

324324
if (expression.NodeType == ExpressionType.Equal)

0 commit comments

Comments
 (0)
Please sign in to comment.