简体   繁体   English

实体框架核心:如何在 Linq 中使用 DateDiff?

[英]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.

相关问题 我可以在没有实体框架的情况下使用LINQ吗? - Can I use LINQ without Entity Framework? 如何使用 Entity Framework Core 中的 Join() 方法将 Sql 查询转换为 Linq 及其等效项 - How can I convert Sql query to Linq and its equivalent with Join() method in Entity Framework Core 如何使用LINQ在实体框架中执行这种类型的JOIN - How can I perform this type of JOIN in Entity framework with LINQ 如何在LINQ to Entity Framework中使用SQL通配符 - How to use SQL wildcards in LINQ to Entity Framework 如何申请sql加入Linq Entity Framework Core - How to apply sql join in Linq Entity Framework Core 如何使用与Entity Framework一起使用表的存储过程 - How can I use a stored procedure that uses tables with Entity Framework 如何使用 Entity Framework Core 验证是否可以在 SQL 级别添加记录 - How can I validate if a record can be added at the SQL level using Entity Framework Core 实体框架:如何将分组依据转换为实体框架声明 - Entity Framework : How can i translate group by to entity framework statement 我可以在表中的多个列上引用单个外键吗? 如果是,如何在实体框架核心中配置它 - Can I reference single foreign key on multiple columns in a table? If yes, how to configure it in Entity Framework core 如何在 Entity Framework Core 2.1 中优化缓慢(不那么)复杂的查询 - How can I optimize slow (not-so) complex queries in Entity Framework Core 2.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM