我執行使用實體框架的服務器端分頁,並有下面的代碼LINQ到實體排序依據評估時的KeySelectors傳遞
DbQuery<T> query = Context.Set<T>();
query = IncludeNavigationProperties(query, includedProperties);
var result = query.OrderBy(arg => arg.DatabaseId)
.Skip((pageNumber - 1)*pageSize)
.Take(pageSize).ToList();
產生我僅查詢所需數據的SQL(使用SQL查詢檢查早Server事件探查器)
SELECT TOP (21)
[Extent1].[DatabaseId] AS [DatabaseId],
...[other props here]...
FROM (SELECT [Extent1].[DatabaseId] AS [DatabaseId], ...[other props here]..., row_number() OVER (ORDER BY [Extent1].[DatabaseId] ASC) AS [row_number]
FROM [dbo].[Table] AS [Extent1]
) AS [Extent1]
WHERE [Extent1].[row_number] > 84
ORDER BY [Extent1].[DatabaseId] ASC
於是我決定重新使用更多的情況下,這方法並傳遞keySelector
作爲一個外部變量:
DbQuery<T> query = Context.Set<T>();
query = IncludeNavigationProperties(query, includedProperties);
var result = query.OrderBy(keySelector)
.Skip((pageNumber - 1)*pageSize)
.Take(pageSize).ToList();
其中
Func<T, int> keySelector = arg => arg.DatabaseId;
但它突然產生下面的SQL查詢:
SELECT
[Extent1].[DatabaseId] AS [DatabaseId],
...[other props here]...
FROM [dbo].[Table] AS [Extent1]
其中,據我瞭解,查詢表所有數據,然後對其進行處理的服務器。
所以,我有2個問題:
- 爲什麼查詢變化?
- 我該如何修復它(能夠改變
keySelector
並只查詢必要的數據)?