如何将此Sql查询转换为LINQ查询

时间:2014-07-29 15:56:09

标签: sql linq linq-to-sql

任何人都可以帮助将以下给定的sql查询转换为linq。

     exec sp_executesql N'use db;
            WITH Members AS
            (
                select ROW_NUMBER() OVER (ORDER BY id DESC) as row, num
                from  tbl
            )
            Select row,  num
            from Members 
            where row BETWEEN  @InitialRow AND @EndRow order by row ASC;',N'@InitialRow int,@EndRow int',@InitialRow=0,@EndRow=5

- 感谢,

1 个答案:

答案 0 :(得分:1)

这是一种方法(使用@ InitialRow = 0,@ EndRow = 5):

var result = Members
               .OrderByDescending(x => x.id)
               .Take(5)
               .Select((x,i) => 
                   new { row = i, num = x.num });

使用任何值:

var result = Members
               .OrderByDescending(x => x.id)
               .Skip(InitialRow)
               .Take(EndRow-InitialRow)
               .Select((x,i) => 
                   new { row = i+InitialRow, num = x.num });