PomeloFoundation / Pomelo.EntityFrameworkCore.MySql

Entity Framework Core provider for MySQL and MariaDB built on top of MySqlConnector
MIT License
2.68k stars 382 forks source link

Feature Request: DateDiff Function #1837

Open rmocode opened 7 months ago

rmocode commented 7 months ago

Hello,

The MySQLDbFunctionsExtensions class has support for many of the specific date difference operations that MySql supports through its function TIMESTAMPDIFF(TIME_UNIT, startDate,endDate), but it doesn't support all of them. For example, there is no support for using QUARTER as a time metric, which you can do in MySql. Of course, the result for MONTH can be divided by 3, as a workaround, which is fine, but there is another issue:

There is no way to write one LINQ Query that takes a unit of measurement as a parameter to the TIMESTAMPDIFF function. So you have to write multiple LINQ Queries in scenarios where you shouldn't have to. One for dealing with weeks, a fixed interval, and another for months, which is a variable interval, for example.

Solution:

I request an added function, which takes as an additional parameter a string or an enumeration that represents a Time Unit, so that you could determine in advance of a LINQ query what the time interval unit will be. As follows:

public static int DateDiff(string unit, this DbFunctions _, DateTime startDate, DateTime endDate)

Thanks,

lauxjpn commented 5 months ago

The MySQLDbFunctionsExtensions class has support for many of the specific date difference operations that MySql supports through its function TIMESTAMPDIFF(TIME_UNIT, startDate,endDate), but it doesn't support all of them. For example, there is no support for using QUARTER as a time metric, which you can do in MySql.

1876 will address this first part of your request. It will probably be backported to 8.0.

(We will address the second part in a different PR.)

lauxjpn commented 5 months ago

Reopened to track implementing the second part of the proposal.