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 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- oracle区分大小写的原因? 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在多列上分布任意行 2021-01-01