Skip to content

Fix enum detection when using OfType for Linq provider #2651

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 1 commit into from
Jan 26, 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
2 changes: 2 additions & 0 deletions src/NHibernate.DomainModel/Northwind/Entities/Animal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
Expand Down
5 changes: 4 additions & 1 deletion src/NHibernate.DomainModel/Northwind/Mappings/Animal.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<joined-subclass name="Reptile">
<key column="Animal"/>
<property name="BodyTemperature"/>
<property name="Enum1" type="NHibernate.DomainModel.Northwind.Entities.EnumStoredAsStringType, NHibernate.DomainModel"
formula="('Unspecified')" insert="false" update="false">
</property>

<joined-subclass name="Lizard">
<key column="Reptile"/>
Expand All @@ -39,4 +42,4 @@
</joined-subclass>
</joined-subclass>
</class>
</hibernate-mapping>
</hibernate-mapping>
13 changes: 13 additions & 0 deletions src/NHibernate.Test/Linq/ParameterTypeLocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public void EqualStringEnumTest()
);
}

[Test]
public void SubClassStringEnumTest()
{
AssertResults(
new Dictionary<string, Predicate<IType>>
{
{"0", o => o is EnumStoredAsStringType}
},
db.Animals.Where(o => o.Children.OfType<Reptile>().Any(r => r.Enum1 == EnumStoredAsString.Unspecified)),
db.Animals.Where(o => o.Children.OfType<Reptile>().Any(r => EnumStoredAsString.Unspecified == r.Enum1))
);
}

[Test]
public void EqualsMethodStringTest()
{
Expand Down
7 changes: 7 additions & 0 deletions src/NHibernate/Util/ExpressionsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -716,6 +717,12 @@ protected override Expression VisitQuerySourceReference(QuerySourceReferenceExpr

protected override Expression VisitSubQuery(SubQueryExpression expression)
{
var ofTypeOperator = expression.QueryModel.ResultOperators.OfType<OfTypeResultOperator>().FirstOrDefault();
if (ofTypeOperator != null)
{
_convertType = ofTypeOperator.SearchedItemType;
}

base.Visit(expression.QueryModel.SelectClause.Selector);
return expression;
}
Expand Down