Pomelo.EntityFrameworkCore.MySql icon indicating copy to clipboard operation
Pomelo.EntityFrameworkCore.MySql copied to clipboard

TimeSpan.TotalHours could not be translated

Open Lyra1337 opened this issue 2 months ago • 7 comments

Steps to reproduce

  1. 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';
  1. Scaffold new context
  2. 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.

Lyra1337 avatar Apr 19 '24 12:04 Lyra1337