有個需求是要統計事件的起始時間與結束時間差的平均值,原本想說下面這個寫法可以動:
1 |
|
結果出現以下錯誤,看來這個寫法是沒辦法順利轉成 SQL
The LINQ expression '((EntityShaperExpression:
EntityType: Event
ValueBufferExpression:
(ProjectionBindingExpression: EmptyProjectionMember)
IsNullable: False
).BeginTime.Value - (EntityShaperExpression:
EntityType: Event
ValueBufferExpression:
(ProjectionBindingExpression: EmptyProjectionMember)
IsNullable: False
).EndTime.Value).Days' could not be translated.
Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
最後發現 DbFunctions 提供許多的 Datediff 方法
於是將程式碼改成以下寫法,問題解決
1 |
|