當我執行下面的代碼時,我得到這個錯誤,任何想法如何解決它?LINQ to Entities不能識別方法'Int32 Min(Int32,Int32)'?
LINQ to Entities does not recognize the method 'Int32 Min(Int32, Int32)' method, and this method cannot be translated into a store expression.
result = items.ToList()
.Select(b => new BatchToWorkOnModel()
{
BatchID = b.Batch.ID,
SummaryNotes = b.Batch.Notes,
RowVersion = b.Batch.RowVersion,
Items = items
.Select(i => new ItemToWorkOnModel()
{
SupplierTitle = i.Title,
ItemID = i.ID,
BatchID = i.BatchID ?? 0,
ItemDate = i.PubDate,
// KB - Issue 276 - Return the correct Outlet name for each item
Outlet = i.Items_SupplierFields != null ? i.Items_SupplierFields.SupplierMediaChannel != null ? i.Items_SupplierFields.SupplierMediaChannel.Name : null : null,
Status = ((short)ItemStatus.Complete == i.StatusID ? "Done" : "Not done"),
NumberInBatch = i.NumInBatch,
Text = string.IsNullOrEmpty(i.Body) ? "" : i.Body.Substring(0, Math.Min(i.Body.Length, 50)) + (i.Body.Length < 50 ? "" : "..."),
IsRelevant = i.IsRelevant == 1,
PreviouslyCompleted = i.PreviouslyCompleted > 0 ? true : false
}).ToList()
})
.FirstOrDefault();
我原則上同意你的意見。然而再來看看源代碼。鑑於'.ToList()'的數量,我們應該運行LinqToObject而不是LinqToEF。我不確定源代碼是否正確... – Aron
@Aron主查詢中的第一個ToList()不會影響子查詢(它基本上只是將整個表讀入一個List),並且子查詢包含ToList()_after_所有選擇都已完成,因此它將使用Linq to Entities進行查詢/投影。 –
我注意到子屬性可能不會被加載,因此會產生n + 1個查詢。不過,我認爲(取決於實施)該屬性可能實現爲一個List <>。這表明子列表是通過Lazy Loading獲取的,而不是顯式的EF查詢。同樣,結果是List上的投影,而不是DbSet/DbQuery/ObjectSet/ObjectQuery。 – Aron