Pomelo.EntityFrameworkCore.MySql
Pomelo.EntityFrameworkCore.MySql copied to clipboard
TimeSpan.TotalHours could not be translated
Steps to reproduce
- Create a Database:
CREATE DATABASE test1;
USE test1;
CREATE TABLE `test` (
`Id` INT NOT NULL,
`Time` TIME NULL,
PRIMARY KEY (`Id`)
)
COLLATE='utf8mb4_general_ci';
- Scaffold new context
- Try to sum all values of the Time column
context.Test.Sum(x => x.Time.TotalHours)
neither works:
context.Test.Sum(x => EF.Functions.DateDiffMinute(x.Time.TotalHours, TimeSpan.Zero) / 60d)
The issue
System.InvalidOperationException: The LINQ expression [...] 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Further technical details
Operating system: Windows / Linux Pomelo.EntityFrameworkCore.MySql version: 8.0.0 Microsoft.AspNetCore.App version: 8.0.200
So, it's dear to me that TotalHours
can't be mapped.
Is there an other way to sum up all time valued-rows?
Can I add the function TIME_TO_SEC
to the EF Functions to call it an then divide by 3600 to get the hours?
Any help will be appreciated.
Thank you in advance.