Skip to content

LINQ: Casting from object to TimeSpan throws #3005

Closed
@maikschott

Description

@maikschott

I updated from 5.2.3 to 5.3.10 and noticed a regression when doing following LINQ query:

session.Query<Entity>().Select(x => (TimeSpan)(object)x.Duration).ToArray();

will throw System.NotSupportedException: Don't currently support idents of type TimeSpan.
The same happens when casting to TimeSpan?.

After trying each release I found that this stopped working beginning with 5.3.0.

Stacktrace
Unhandled exception. System.NotSupportedException: Don't currently support idents of type TimeSpan
   at NHibernate.Hql.Ast.HqlIdent..ctor(IASTFactory factory, Type type)
   at NHibernate.Hql.Ast.HqlCast..ctor(IASTFactory factory, HqlExpression expression, Type type)
   at NHibernate.Hql.Ast.HqlTreeBuilder.Cast(HqlExpression expression, Type type)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionVisitor.VisitUnaryExpression(UnaryExpression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionVisitor.VisitExpression(Expression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionVisitor.Visit(Expression expression)
   at NHibernate.Linq.Visitors.SelectClauseVisitor.Visit(Expression expression)
   at NHibernate.Linq.Visitors.SelectClauseVisitor.VisitSelector(Expression expression)
   at NHibernate.Linq.Visitors.QueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel)
   at Remotion.Linq.Clauses.SelectClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel)
   at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
   at NHibernate.Linq.Visitors.QueryModelVisitor.Visit()
   at NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel, VisitorParameters parameters, Boolean root, Nullable`1 rootReturnType)
   at NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory, Boolean filter)
   at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.QueryExpressionPlan.CreateTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.QueryExpressionPlan..ctor(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
   at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
   at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
   at NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query)
   at NHibernate.Linq.DefaultQueryProvider.ExecuteList[TResult](Expression expression)
   at NHibernate.Linq.NhQueryable`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Program.<Main>$(String[] args) in C:\Users\schott\source\repos\NHibernateTimeSpanBug\NHibernateTimeSpanBug\Program.cs:line 16
Detailed test case
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Tool.hbm2ddl;

var sessionFactory = Fluently.Configure()
  .Database(MsSqlConfiguration.MsSql2012.ConnectionString("Data Source=SQLServer;Initial Catalog=MSTest;User Id=user;Password=pass"))
  .Mappings(m =>
  {
    m.FluentMappings.Add<EntityMap>();
  })
  .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, true))
  .BuildSessionFactory();

using var session = sessionFactory.OpenSession();
var _ = session.Query<Entity>().Select(x => (TimeSpan)(object)x.Duration).ToArray();

class EntityMap : ClassMap<Entity>
{
  internal EntityMap()
  {
    Id(x => x.Id);
    Map(x => x.Duration);
  }
}

class Entity
{
  public virtual int Id { get; set; }
  public virtual TimeSpan Duration { get; set; }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions