Offset/Fetch based paging (Implementation) in EntityFramework (Using LINQ) for SQL Server 2008

蹲街弑〆低调 提交于 2019-11-29 08:32:16

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);

The fix is to modify your EDMX file, using the XML editor, and change the value of ProviderManifestToken from 2012 to 2008. I found that on line 7 in my EDMX file. After saving that change, the paging SQL will be generated using the “old”, SQL Server 2008 compatible syntax.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!