Skip to content

Commit

Permalink
Fix enum detection when using OfType for Linq provider
Browse files Browse the repository at this point in the history
  • Loading branch information
maca88 authored and fredericDelaporte committed Jan 26, 2021
1 parent 5ec4821 commit 6ee61fa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
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

0 comments on commit 6ee61fa

Please sign in to comment.