EF CORE中的DbFunctions.TruncateTime LINQ等效项 [英] DbFunctions.TruncateTime LINQ equivalent in EF CORE

查看:1502
本文介绍了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以及转换为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等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆