Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

LINQ to SQL ogni ennesima riga dalla tabella

A volte, TSQL è la strada da percorrere. Userei ExecuteQuery<T> qui:

    var data = db.ExecuteQuery<SomeObjectType>(@"
SELECT * FROM 
(SELECT *, ROW_NUMBER() OVER (ORDER BY id) AS [__row]
FROM [YourTable]) x WHERE (x.__row % 25) = 1");

Puoi anche sostituire il n :

    var data = db.ExecuteQuery<SomeObjectType>(@"
DECLARE @n int = 2
SELECT * FROM 
(SELECT *, ROW_NUMBER() OVER (ORDER BY id) AS [__row]
FROM [YourTable]) x WHERE (x.__row % @n) = 1", n);