question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LinqToDB.Common.LinqToDBConvertException: Cannot convert value '1/25/1980 12:00:00 AM: System.DateTime' to type 'System.DateOnly'

See original GitHub issue

Not sure if .net 6.0 is completely supported at this point, but I’ve and issue with DateOnly fields. Query fails on materialization

Exception message: Cannot convert value '1/25/1980 12:00:00 AM: System.DateTime' to type 'System.DateOnly'

Stack trace:
LinqToDB.Common.LinqToDBConvertException: Cannot convert value '1/25/1980 12:00:00 AM: System.DateTime' to type 'System.DateOnly'
 ---> System.InvalidCastException: Invalid cast from 'System.DateTime' to 'System.DateOnly'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at System.DateTime.System.IConvertible.ToType(Type type, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at LinqToDB.Common.ConvertBuilder.ConvertDefault(Object value, Type conversionType)
   --- End of inner exception stack trace ---
   at LinqToDB.Common.ConvertBuilder.ConvertDefault(Object value, Type conversionType)
   at lambda_method1066(Closure , IDataReader )
   at LinqToDB.Expressions.ConvertFromDataReaderExpression.ColumnReader.GetValue(IDataReader dataReader)
   at lambda_method1059(Closure , IQueryRunner , IDataReader )
   at LinqToDB.Linq.QueryRunner.Mapper`1.Map(IDataContext context, IQueryRunner queryRunner, IDataReader dataReader)
   at LinqToDB.Linq.QueryRunner.ExecuteQueryAsync[T](Query query, IDataContext dataContext, Mapper`1 mapper, Expression expression, Object[] ps, Object[] preambles, Int32 queryNumber, Func`2 func, TakeSkipDelegate skipAction, TakeSkipDelegate takeAction, CancellationToken cancellationToken)
   at LinqToDB.Linq.QueryRunner.ExecuteQueryAsync[T](Query query, IDataContext dataContext, Mapper`1 mapper, Expression expression, Object[] ps, Object[] preambles, Int32 queryNumber, Func`2 func, TakeSkipDelegate skipAction, TakeSkipDelegate takeAction, CancellationToken cancellationToken)
   at LinqToDB.Linq.QueryRunner.ExecuteQueryAsync[T](Query query, IDataContext dataContext, Mapper`1 mapper, Expression expression, Object[] ps, Object[] preambles, Int32 queryNumber, Func`2 func, TakeSkipDelegate skipAction, TakeSkipDelegate takeAction, CancellationToken cancellationToken)
   at LinqToDB.Linq.ExpressionQuery`1.GetForEachAsync(Action`1 action, CancellationToken cancellationToken)
   at LinqToDB.AsyncExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken token)
   at Vtbf.Contractors.Application.CQRS.ContractorsLegal.ContractorLegalQueryByFilter.Handler.SelectWithSearchQueryAsync(ContractorLegalQueryByFilter request, CancellationToken cancellationToken) in /home/konstantin/sources/....cs:line 82

Steps to reproduce

Fails on materialization

Environment details

linq2db version: 3.6 Database Server: Postgres 14 Database Provider: Npgsql.EntityFrameworkCore.PostgreSQL 6.0.1 Operating system: Ubuntu 21.10 .NET Framework: .net 6.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
viceroypenguincommented, Dec 29, 2022

@SteveDunn I haven’t implemented TimeOnly yet - haven’t found a use for it yet. If you need it, I’ll put something together soon.

1reaction
MaceWinducommented, Dec 9, 2021

You just need to define DateTime => DateOnly conversion in your mapping schema.

// e.g. to Default schema
MappingSchema.Default.SetConverter<DateTime, DateOnly>(dt => DateOnly.FromDateTime(dt));

but yes, we need to embedd those conversions into linq2db at some point.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert System.DateTime? to System.DateOnly?
So DateTime? to DateOnly? The error says: Error CS0029 Cannot implicitly convert type 'System.DateOnly?' to 'System.DateTime?' I can convert ...
Read more >
DateOnly.FromDateTime(DateTime) Method (System)
Returns a DateOnly instance that is set to the date part of the specified dateTime.
Read more >
Date, Time, and Time Zone Enhancements in .NET 6
In this blog post, I'm going to cover the following topics: The new DateOnly and TimeOnly types; Time Zone Conversion APIs; Time Zone...
Read more >
Converting DateOnly and TimeOnly to DateTime and vice ...
Since I have introduced DateOnly and TimeOnly feature of .NET 6 in my previous article( Simplified Date and Time with DateOnly and TimeOnly...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found