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.

Convert.ToDateTime not working

See original GitHub issue

Hi, With the following table:

CREATE TABLE public."Values"
(
    "Guid" bigint NOT NULL,
    "RawValue" text COLLATE pg_catalog."default",
    CONSTRAINT "Values_pkey" PRIMARY KEY ("Guid")
)

The following sample:

    public class EFCoreTestContext : DbContext
    {
        public DbSet<Value> Values { get; set; }

        public static readonly ILoggerFactory DebugLoggerFactory = LoggerFactory.Create(builder => { builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole(); });

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder
            .UseLoggerFactory(DebugLoggerFactory)
            .UseNpgsql("Host=localhost;Database=EFCoreTest;Username=test;Password=test");

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder
                .Entity<Value>(eb =>
                {
                    eb.ToTable("Values");
                    eb.HasKey(e => e.Guid);
                });
        }
    }

    public class Value
    {
        public long Guid { get; set; }

        public string RawValue { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new EFCoreTestContext())
            {
                var r = (from e in db.Values
                         select new
                         {
                             Date = Convert.ToDateTime(e.RawValue)
                         })
                         .OrderBy(e => e.Date)
                         .ToList();
            }

            Console.ReadKey();
        }
    }

I’m getting this error:

System.InvalidOperationException: The LINQ expression ‘DbSet<Value> .OrderBy(v => Convert.ToDateTime(v.RawValue))’ could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

I was expecting this kind of query translated:

SELECT v."Guid", cast (v."RawValue" as timestamp with time zone) AS "Date"
FROM "Values" AS v
ORDER BY v."RawValue"

The values stored on the table in my case are something like this when they are DateTimes: “2020-01-21T14:33:29-03:00” I searched for some cast method like the EF.Functions.ILike method but had no luck.

Thanks in advance,

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
smitpatelcommented, May 27, 2020

We had discussions about translating ToString method on object which would be whatever string representation of the value in database is.

1reaction
ajcvickerscommented, May 27, 2020

@roji I think we said that we would generate simple casts in the database, but would not try to be specific about how the conversions worked. However, I’m not sure either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DateTime.Parse or Convert.ToDateTime is not working
datett= DateTime.Parse(txt.Text),. It says FormatException. I debugged and also tried Convert.ToDatetime, same exception raised. The ...
Read more >
Convert string to DateTime not working - Studio
Sometimes CDate works, and sometimes I get a “Cannot assign CDate(x) to date”. Then I use TryParse, and it works. However, sometimes TryParse ......
Read more >
Convert.ToDateTime Method (System)
TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.
Read more >
Convert.ToDateTime() not working on Server.. Give format ...
Coding example for the question Convert.ToDateTime() not working on Server.. Give format exception-C#.
Read more >
String To DateTime Conversion In C#
In this scenario, we need to convert a string value to a DateTime object and then use the WeekDay property(obj.WeekDay) to determine the...
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