From 6ee61faee5ca89a473e00526c3cf5aac31ea6f85 Mon Sep 17 00:00:00 2001 From: maca88 Date: Sun, 24 Jan 2021 16:40:35 +0100 Subject: [PATCH] Fix enum detection when using OfType for Linq provider --- .../Northwind/Entities/Animal.cs | 2 ++ .../Northwind/Mappings/Animal.hbm.xml | 5 ++++- .../Linq/ParameterTypeLocatorTests.cs | 13 +++++++++++++ src/NHibernate/Util/ExpressionsHelper.cs | 7 +++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/NHibernate.DomainModel/Northwind/Entities/Animal.cs b/src/NHibernate.DomainModel/Northwind/Entities/Animal.cs index 21fac97d4ae..384e6e760d5 100644 --- a/src/NHibernate.DomainModel/Northwind/Entities/Animal.cs +++ b/src/NHibernate.DomainModel/Northwind/Entities/Animal.cs @@ -19,6 +19,8 @@ public class Animal public abstract class Reptile : Animal { public virtual double BodyTemperature { get; set; } + + public virtual EnumStoredAsString Enum1 { get; set; } } public class Lizard : Reptile { } diff --git a/src/NHibernate.DomainModel/Northwind/Mappings/Animal.hbm.xml b/src/NHibernate.DomainModel/Northwind/Mappings/Animal.hbm.xml index 12e6bcd1e23..7cf325b54aa 100644 --- a/src/NHibernate.DomainModel/Northwind/Mappings/Animal.hbm.xml +++ b/src/NHibernate.DomainModel/Northwind/Mappings/Animal.hbm.xml @@ -19,6 +19,9 @@ + + @@ -39,4 +42,4 @@ - \ No newline at end of file + diff --git a/src/NHibernate.Test/Linq/ParameterTypeLocatorTests.cs b/src/NHibernate.Test/Linq/ParameterTypeLocatorTests.cs index 48cc2e14048..50be6a9e470 100644 --- a/src/NHibernate.Test/Linq/ParameterTypeLocatorTests.cs +++ b/src/NHibernate.Test/Linq/ParameterTypeLocatorTests.cs @@ -84,6 +84,19 @@ public void EqualStringEnumTest() ); } + [Test] + public void SubClassStringEnumTest() + { + AssertResults( + new Dictionary> + { + {"0", o => o is EnumStoredAsStringType} + }, + db.Animals.Where(o => o.Children.OfType().Any(r => r.Enum1 == EnumStoredAsString.Unspecified)), + db.Animals.Where(o => o.Children.OfType().Any(r => EnumStoredAsString.Unspecified == r.Enum1)) + ); + } + [Test] public void EqualsMethodStringTest() { diff --git a/src/NHibernate/Util/ExpressionsHelper.cs b/src/NHibernate/Util/ExpressionsHelper.cs index 6f9f290aa72..a5874cf94f0 100644 --- a/src/NHibernate/Util/ExpressionsHelper.cs +++ b/src/NHibernate/Util/ExpressionsHelper.cs @@ -16,6 +16,7 @@ using NHibernate.Type; using Remotion.Linq.Clauses; using Remotion.Linq.Clauses.Expressions; +using Remotion.Linq.Clauses.ResultOperators; using Remotion.Linq.Parsing; namespace NHibernate.Util @@ -716,6 +717,12 @@ protected override Expression VisitQuerySourceReference(QuerySourceReferenceExpr protected override Expression VisitSubQuery(SubQueryExpression expression) { + var ofTypeOperator = expression.QueryModel.ResultOperators.OfType().FirstOrDefault(); + if (ofTypeOperator != null) + { + _convertType = ofTypeOperator.SearchedItemType; + } + base.Visit(expression.QueryModel.SelectClause.Selector); return expression; }