DbFunctions.TruncateTime LINQ equivalent in EF CORE(EF CORE 中的 DbFunctions.TruncateTime LINQ 等效项)
问题描述
我的 .net 应用程序中有以下功能 LINQ
I have the following functioning LINQ in my .net app
public ActionResult Index()
{
Dictionary<DateTime?, List<Event>> result;
result = (from events in db.Events.Include("Activity")
where events.IsActive
group events by DbFunctions.TruncateTime(events.DateTimeFrom) into dateGroup
select new { EventDate = dateGroup.Key, Events = dateGroup.ToList() }).ToDictionary(x => x.EventDate, x => x.Events);
return View(result);
}
当我在 EF Core 中使用它时,我无法使用 DbFunctions.我如何重写它以使其在 Microsoft.EntityFrameworkCore 中工作?如果这有所作为,我正在使用 SQLite.
When I use this in EF Core, I can't use DbFunctions. How can I rewrite this to make it work in Microsoft.EntityFrameworkCore ? I am using SQLite if that makes a difference.
推荐答案
在 EF6 中使用 DbFunctions.TruncateTime
代替 DateTime.Date
属性,因为某些原因后者不支持.
In EF6 DbFunctions.TruncateTime
is used instead of DateTime.Date
property because for some reason the later is not supported.
在 EF Core 中不需要前者,因为 DateTime.Date
现在可以正确识别和翻译.
In EF Core the former is not needed simply because DateTime.Date
now is recognized and translated correctly.
group events by events.DateTimeFrom.Date into dateGroup
不幸的是,目前还没有支持什么的文档,所以作为一般的经验法则,总是尝试相应的 CLR 方法/属性(如果有)并检查它是否转换为 SQL 以及如何转换.
Unfortunately there is no documentation (yet) of what is supported, so as a general rule of thumb, always try the corresponding CLR method/property (if any) and check if it translates to SQL and how.
这篇关于EF CORE 中的 DbFunctions.TruncateTime LINQ 等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:EF CORE 中的 DbFunctions.TruncateTime LINQ 等效项


基础教程推荐
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01