任何人都知道如何编写一个LINQ to sql语句从表中返回第n行?我需要在分页数据网格中的每个页面的顶部获取该项目的标题,以便快速的用户扫描.所以如果我想要第一个记录,那么每3个之后,从以下名字:
艾米,埃里克,杰森,乔,约翰,乔希,马里贝尔,保罗,史蒂夫,汤姆
我会得到艾米,马里贝尔和汤姆.
我怀疑这可以完成… LINQ to sql语句已经调用了ROW_NUMBER()sql函数和排序和分页.我只是不知道如何回到第n个项目. sql语句将像WHERE ROW_NUMBER MOD 3 = 0,但我不知道用于获取正确的sql的LINQ语句.
解决方法@H_404_10@
有时,Tsql是要走的路.我将使用ExecuteQuery< T>这里:
var data = db.ExecuteQuery<SomeObjectType>(@"
SELECT * FROM
(SELECT *,ROW_NUMBER() OVER (ORDER BY id) AS [__row]
FROM [YourTable]) x WHERE (x.__row % 25) = 1");
你也可以换出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);
var data = db.ExecuteQuery<SomeObjectType>(@" SELECT * FROM (SELECT *,ROW_NUMBER() OVER (ORDER BY id) AS [__row] FROM [YourTable]) x WHERE (x.__row % 25) = 1");
你也可以换出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);