[英]EF core 3 nested group by after restricting client evaluation
升級到實體框架核心 3 后,下面的代碼在嘗試獲取h.CreationDateTime.Hour
時拋出異常,在 EF 核心 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();
有沒有辦法可以重寫此代碼以提供相同的 output。 任何幫助表示贊賞。
根據 Microsoft 文檔,使用.AsEnumerable() 將其切換到 LINQ 到 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.