简体   繁体   English

EF core 3嵌套组通过限制客户端评估后

[英]EF core 3 nested group by after restricting client evaluation

After upgrading to entity framework core 3 the code below throws an exception when trying to get h.CreationDateTime.Hour , in EF core 2 this part was executed on the client.升级到实体框架核心 3 后,下面的代码在尝试获取h.CreationDateTime.Hour时抛出异常,在 EF 核心 2 中,这部分是在客户端上执行的。 but now when EF Core 3.0 detects expressions that can't be translated anywhere else in the query, it throws a runtime exception.但是现在,当 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();

Is there a way this code can be rewritten to give the same output.有没有办法可以重写此代码以提供相同的 output。 any help is appreciated.任何帮助表示赞赏。

Using.AsEnumerable() switches it to LINQ to object according to Microsoft docs.根据 Microsoft 文档,使用.AsEnumerable() 将其切换到 LINQ 到 object。 https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/ 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM