EF Core 3 嵌套分组依据限制客户评估后

EF core 3 nested group by after restricting client evaluation

升级到 entity framework core 3 后,下面的代码在尝试获取 h.CreationDateTime.Hour 时抛出异常,在 EF core 2 中,这部分是在客户端上执行的。但现在当 EF Core 3.0 检测到无法在查询中的其他任何地方翻译的表达式时,它会引发运行时异常。

var x = dbContext.data.GroupBy(c => c.CreationDateTime.Day)
                            .Select(d => new
                            {
                                day = d.Key,
                                hours = d.GroupBy(h => h.CreationDateTime.Hour).Select(h => new
                                {
                                    hour= h.Key,
                                    count = h.Count()
                                }).ToList()
                            }).ToList();

有没有办法可以重写此代码以提供相同的输出。感谢您的帮助。

根据 Microsoft 文档,使用 .AsEnumerable() 将其切换为 LINQ to object。 https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/

 var x = dbContext.data.GroupBy(c => c.CreationDateTime.Day)
                        .AsEnumerable()
                        .Select(d => new
                        {
                            day = d.Key,
                            hours = d.GroupBy(h => h.CreationDateTime.Hour).Select(h 
                            => new
                            {
                                hour= h.Key,
                                count = h.Count()
                            }).ToList()
                        }).ToList();