Carl Daniel [VC++ MVP]
...but be aware that in all likelihood, this will return the entire query
result into memory, then discard the first m rows, and finally retun the
next n rows. The select ... offset... limit is Oracle-specific SQL
syntax,
so I wouldn't assume that Linq to SQL knows about it.
Fortunately it's easy to check this, and in LINQ to SQL against SQL
Server 2005 at least, it works well. I tried this:
var q = db.Customers.Skip(3).Take(5);
foreach (Customer cust in q)
{
Console.WriteLine (cust.CompanyName);
}
and the SQL executed was:
SELECT [t1].[CustomerID], [t1].[CompanyName], [t1].[ContactName], [t1].
[ContactTitle], [t1].[Address], [t1].[City], [t1].[Region], [t1].
[PostalCode], [t1].[Country], [t1].[Phone], [t1].[Fax]
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY [t0].[CustomerID], [t0].
[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address],
[t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].
[Phone], [t0].[Fax]) AS [ROW_NUMBER], [t0].[CustomerID], [t0].
[CompanyName], [t0].[ContactName], [t0].[ContactTitle],
[t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].
[Country],[t0].[Phone], [t0].[Fax]
FROM [dbo].[Customers] AS [t0]
) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1
ORDER BY [t1].[ROW_NUMBER]
It's not exactly neat, but the query plan looks reasonable, and it
certainly avoids returning everything to the client.
(Note that even if it *did* do everything in the client, it wouldn't
have to load all the results into memory before starting to skip
things.)