Offset/Fetch based paging (Implementation) in Enti

2019-01-25 23:25发布

I am using SQL Server 2008 and Entity Framework 6.1.3. I wish to implement pagination of data based on OFFSET/FETCH rather than Take() & Skip().

I searched online with no luck. Some posts suggested migrating to SQL Server 2012. Which is not an option in my case.

Can someone suggest how to use OFFSET/FETCH with SQL Server 2008 and EF 6.1.3

2条回答
我只想做你的唯一
2楼-- · 2019-01-26 00:02

This is possible with Entity Framework 6.1.2 and above so you should be OK to use it in your project. The standard Skip and Take methods can't be captured in the same way as others. There are now two additional overload of the Skip/Take methods that take lambdas, so instead of this:

var results = context.MyTable
    .Skip(10)
    .Take(5);

Do this:

var results = context.MyTable
    .Skip(() => 10)
    .Take(() => 5);
查看更多
我只想做你的唯一
3楼-- · 2019-01-26 00:09

This example may helps you to use OffSET In Sql server

DECLARE @PageNo INT=NULL, 
@RowsPerPage INT=2
;WIth cte(id, name, qty, price)
AS
(
SELECT 2,'a', 2, 20 UNION ALL
SELECT 3,'d', 2, 10 UNION ALL
SELECT 4,'b', 3, 60
)
,cte2 AS (SELECT id, name, qty, price , 1 AS n FROM cte
UNION ALL
SELECT id, name, qty, price , n+1 From cte2 t
WHERE n< t.qty
)
SELECT
  ROW_NUMBER() OVER (ORDER BY (SELECT 1) ) AS Seq,
  id,
  name,
  qty,
  price
FROM cte2
ORDER BY 2, 3, n

OFFSET (COALESCE((CASE  WHEN @PageNo <= 0 THEN 1
                        WHEN @PageNo > 0 THEN @PageNo END), 1) - 1) ROWS
FETCH NEXT @RowsPerPage ROWS ONLY
查看更多
登录 后发表回答