[英]Entity Framework Core : how can I use DateDiff in Linq?
I want to create a query in EF Core like this:我想在 EF Core 中创建一个查询,如下所示:
SELECT SUM(DATEDIFF(DAY, FromDate, ToDate)) AS Diff
FROM Table1
The above query is for T-SQL - how can I create a query in Linq?上述查询适用于 T-SQL - 如何在 Linq 中创建查询?
Using DbFunctions
, accessed via EF.Functions
, you can call DateDiffDay
:使用通过
DbFunctions
访问的EF.Functions
,您可以调用DateDiffDay
:
var ans = from t in Table1
group t by 1 into tg
select tg.Sum(r => EF.Functions.DateDiffDay(r.FromDate, r.ToDate));
My SQL to LINQ Recipe might help you with some translation issues in the future.我的SQL 到 LINQ 食谱将来可能会帮助您解决一些翻译问题。
PS: As noted by @B12Toaster, particular EF.Functions
methods are provider specific and DataDiffDay
is specific to MS SQL Server. PS:正如@B12Toaster 所指出的,特定的
EF.Functions
方法是特定于提供程序的,而DataDiffDay
是特定于 MS SQL 服务器的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.