Convert.ToDateTime not working
See original GitHub issueHi, 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:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We had discussions about translating ToString method on object which would be whatever string representation of the value in database is.
@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.