Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite EF核心中的DbFunctions.TruncateTime LINQ等效项_Sqlite_Linq_Entity Framework Core - Fatal编程技术网

Sqlite EF核心中的DbFunctions.TruncateTime LINQ等效项

Sqlite EF核心中的DbFunctions.TruncateTime LINQ等效项,sqlite,linq,entity-framework-core,Sqlite,Linq,Entity Framework Core,在我的.net应用程序中,我有以下功能正常的LINQ public ActionResult Index() { Dictionary<DateTime?, List<Event>> result; result = (from events in db.Events.Include("Activity") where events.IsActive

在我的.net应用程序中,我有以下功能正常的LINQ

    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);
    }
public ActionResult Index()
{
词典结果;
结果=(来自db.events.Include(“活动”)中的事件)
events.IsActive在哪里
按DbFunctions.TruncateTime(events.DateTimeFrom)将事件分组到dateGroup中
选择new{EventDate=dateGroup.Key,Events=dateGroup.ToList()}).ToDictionary(x=>x.EventDate,x=>x.Events);
返回视图(结果);
}

当我在efcore中使用它时,我不能使用DbFunctions。如何重写此文件以使其在Microsoft.EntityFrameworkCore中工作?我正在使用SQLite,如果这有区别的话

EF Core尚不支持DBFunction。但是,您可以使用“原始Sql查询”

您可以找到


您还可以在EF6
DbFunctions中跟踪EF Core的DbFunctions。TruncateTime
被用来代替
DateTime.Date
属性,因为出于某种原因,后者不受支持

在EF Core中,不需要前者,因为
DateTime.Date
现在已被正确识别和翻译

group events by events.DateTimeFrom.Date into dateGroup

不幸的是,目前还没有支持什么的文档,所以作为一般的经验法则,请始终尝试相应的CLR方法/属性(如果有),并检查它是否转换为SQL以及如何转换

我也设法在Lambda中重写了它,并使其异步。看起来也一样

var temp = await _context.Events.Where(x => x.IsActive)
            .Include(a => a.Activity)
            .GroupBy(x => x.DateTimeFrom.Date)
            .Select(g => new { EventDate = g.Key, Events = g.ToList() }).ToDictionaryAsync(x => x.EventDate, x => x.Events);

EF Core 2.0现在支持将数据库函数映射到上下文中的静态方法


查看此处的“数据库标量函数映射”部分-

要在ASP.NET CORE中使用DbFunctions,必须创建一个对象

var DbF = Microsoft.EntityFrameworkCore.EF.Functions;
现在你可以很容易地使用它了

var now = DateTime.Now;

int count = db.Tbl.Count(w => DbF.DateDiffDay(now, w.RegisterDate) <= 3);
var now=DateTime.now;
int count=db.Tbl.count(w=>DbF.DateDiffDay(现在,w.RegisterDate)EF核心3.0

我终于找到了一个有效的答案,问题是我想在数据库中的DateTime列上按日期分组

对我来说,关键是使用EF.Property函数。这允许类具有DateTime属性,该属性用于添加该级别的数据,但下面允许我将其重新定义为日期。但是..我怀疑如果我在类上贴花该属性,它将允许我使用不允许使用的.Date函数告诉我该做什么

因此,解决方案可能是在模型上定义属性,或者在查询中使用下面的定义

EF.财产日期

完整代码

var myData = _context.ActivityItems
                            .GroupBy(a => new { nlid = a.lid, nsd = EF.Property<DateTime>(a, "dt").Date })
                            .Select(g => new
                            {
                                g.Key.nlid,
                                g.Key.nsd,
                                cnt = g.Count()
                            });
var myData=\u context.ActivityItems
.GroupBy(a=>new{nlid=a.lid,nsd=EF.Property(a,“dt”).Date})
.选择(g=>new
{
g、 Key.nlid,
g、 Key.nsd,
cnt=g.计数()
});

+100用于提供跟踪链接!现在我看到.NET Core 2.0具有EF.Functions。因此,您使用了
DateTime
对象的
.Date
属性,它在EF Core?Brilliant中工作。在我的例子中,这有助于使用npgsql在DateTimeOffset属性上应用日期。在.NET Core 3+中,您可以使用EF.Functions。如何分配将一个对象赋给一个变量会导致它使用不同的方法/更改其在C#?@Reinis中的可访问性在运行时此对象会被
QueryCompiler
转换为sql代码,但为什么必须进行赋值?将
EF.Functions
对象赋给变量会如何更改
TruncateTime
方法是否有效可访问?为什么不直接使用对象而不将其分配给变量?@Reinis这是因为我的代码正在澄清。您也可以直接使用
EF.Functions.DateDiffDay()