diff --git a/src/NHibernate.Test/Async/Linq/ConstantTest.cs b/src/NHibernate.Test/Async/Linq/ConstantTest.cs index b5f0f05037d..35e8c9bfc3d 100644 --- a/src/NHibernate.Test/Async/Linq/ConstantTest.cs +++ b/src/NHibernate.Test/Async/Linq/ConstantTest.cs @@ -11,6 +11,7 @@ using System.Collections.Generic; using System.Linq; using NHibernate.DomainModel.Northwind.Entities; +using NHibernate.Linq.Visitors; using NUnit.Framework; using NHibernate.Linq; @@ -179,7 +180,6 @@ public int GetItemValue(Product p) // Adapted from NH-2500 first test case by Andrey Titov (file NHTest3.zip) [Test] - [Ignore("Not fixed yet")] public async Task ObjectConstantsAsync() { var builder = new InfoBuilder(1); @@ -200,7 +200,6 @@ private int TestFunc(Product item, int closureValue) // Adapted from NH-3673 [Test] - [Ignore("Not fixed yet")] public async Task ConstantsInFuncCallAsync() { var closureVariable = 1; diff --git a/src/NHibernate.Test/Linq/ConstantTest.cs b/src/NHibernate.Test/Linq/ConstantTest.cs index a30118e7283..24d474951e0 100644 --- a/src/NHibernate.Test/Linq/ConstantTest.cs +++ b/src/NHibernate.Test/Linq/ConstantTest.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using NHibernate.DomainModel.Northwind.Entities; +using NHibernate.Linq.Visitors; using NUnit.Framework; namespace NHibernate.Test.Linq @@ -167,7 +168,6 @@ public int GetItemValue(Product p) // Adapted from NH-2500 first test case by Andrey Titov (file NHTest3.zip) [Test] - [Ignore("Not fixed yet")] public void ObjectConstants() { var builder = new InfoBuilder(1); @@ -188,7 +188,6 @@ private int TestFunc(Product item, int closureValue) // Adapted from NH-3673 [Test] - [Ignore("Not fixed yet")] public void ConstantsInFuncCall() { var closureVariable = 1; @@ -201,5 +200,24 @@ public void ConstantsInFuncCall() Assert.That(v1, Is.EqualTo(1), "v1"); Assert.That(v2, Is.EqualTo(2), "v2"); } + + [Test] + public void ConstantInWhereDoesNotCauseManyKeys() + { + var q1 = (from c in db.Customers + where c.CustomerId == "ALFKI" + select c); + var q2 = (from c in db.Customers + where c.CustomerId == "ANATR" + select c); + var parameters1 = ExpressionParameterVisitor.Visit(q1.Expression, Sfi); + var k1 = ExpressionKeyVisitor.Visit(q1.Expression, parameters1); + var parameters2 = ExpressionParameterVisitor.Visit(q2.Expression, Sfi); + var k2 = ExpressionKeyVisitor.Visit(q2.Expression, parameters2); + + Assert.That(parameters1, Has.Count.GreaterThan(0), "parameters1"); + Assert.That(parameters2, Has.Count.GreaterThan(0), "parameters2"); + Assert.That(k2, Is.EqualTo(k1)); + } } } diff --git a/src/NHibernate/Linq/Visitors/ExpressionKeyVisitor.cs b/src/NHibernate/Linq/Visitors/ExpressionKeyVisitor.cs index 1789357cfb0..ee053687006 100644 --- a/src/NHibernate/Linq/Visitors/ExpressionKeyVisitor.cs +++ b/src/NHibernate/Linq/Visitors/ExpressionKeyVisitor.cs @@ -109,19 +109,23 @@ protected override Expression VisitConstant(ConstantExpression expression) { _string.Append("NULL"); } - else + else if (expression.Value is IQueryable) { - var value = expression.Value as IEnumerable; - if (value != null && !(value is string) && !(value is IQueryable)) - { - _string.Append("{"); - _string.Append(String.Join(",", value.Cast())); - _string.Append("}"); - } - else + _string.Append(expression.Value); + } + else if (expression.Value is IEnumerable enumerable && !(enumerable is string)) + { + _string.Append("{"); + foreach (var value in enumerable) { - _string.Append(expression.Value); + _string.Append(value).Append("#").Append(value.GetHashCode()).Append(","); } + + _string.Append("}"); + } + else + { + _string.Append(expression.Value).Append("#").Append(expression.Value.GetHashCode()); } } @@ -163,20 +167,21 @@ protected override Expression VisitMethodCall(MethodCallExpression expression) { var old = insideSelectClause; - switch (expression.Method.Name) - { - case "First": - case "FirstOrDefault": - case "Single": - case "SingleOrDefault": - case "Select": - case "GroupBy": - insideSelectClause = true; - break; - default: - insideSelectClause = false; - break; - } + if (expression.Method.DeclaringType?.Namespace == "System.Linq") + switch (expression.Method.Name) + { + case "First": + case "FirstOrDefault": + case "Single": + case "SingleOrDefault": + case "Select": + case "GroupBy": + insideSelectClause = true; + break; + default: + insideSelectClause = false; + break; + } Visit(expression.Object); _string.Append('.');