views:

18

answers:

1

I would like to query an EF 4 entity set for every nth row, such that I get no more than x results. For example, given a set of 1000 names, give me every 100'th name (sorted by name of course) so that I would have 10 results.

Is this kind of thing possible with Linq to Entites, in any kind of efficient manner?

+1  A: 

Not in L2S, but you could do this with ExecuteStoreQuery:

You want SQL like:

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

(Or substitute a param for my hard-coded 4.)

And then code like:

var q = Context.ExecuteStoreQuery<Foo>(
    sql, params);
Craig Stuntz