0
我想查詢爲每第n行設置一個EF 4實體,以便得到不超過x個結果。例如,給定一組1000個名字,給我每100個名字(按課程名稱排序),以便我有10個結果。如何從一組有序的實體中查詢每個第n個值?
LINQ to Entities可以以任何有效的方式實現這種事情嗎?
我想查詢爲每第n行設置一個EF 4實體,以便得到不超過x個結果。例如,給定一組1000個名字,給我每100個名字(按課程名稱排序),以便我有10個結果。如何從一組有序的實體中查詢每個第n個值?
LINQ to Entities可以以任何有效的方式實現這種事情嗎?
不在L2S,但你可以用ExecuteStoreQuery做到這一點:
你想SQL這樣的:
SELECT
[Project1].[C1] AS [C1],
[Project1].[DepartmentId] AS [DepartmentId],
[Project1].[Code] AS [Code],
[Project1].[DepartmentName] AS [DepartmentName]
FROM (SELECT [Project1].[DepartmentId] AS [DepartmentId], [Project1].[Code] AS [Code], [Project1].[DepartmentName] AS [DepartmentName], [Project1].[C1] AS [C1], row_number() OVER (ORDER BY [Project1].[DepartmentName] ASC) AS [row_number]
FROM (SELECT
[Extent1].[DepartmentId] AS [DepartmentId],
[Extent1].[Code] AS [Code],
[Extent1].[DepartmentName] AS [DepartmentName]
1 AS [C1]
FROM [dbo].[Department] AS [Extent1]
) AS [Project1]
) AS [Project1]
WHERE [Project1].[row_number] % 4 = 0
ORDER BY [Project1].[DepartmentName] ASC
(或替代一個參數有關我的硬編碼4)
而且然後代碼如下:
var q = Context.ExecuteStoreQuery<Foo>(
sql, params);