如何翻译使用ROW_NUMBER()到LINQ查询? [英] How do I translate a query that uses ROW_NUMBER() into linq?
本文介绍了如何翻译使用ROW_NUMBER()到LINQ查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表由三列(SNO,姓名,年龄)的。我从额外的列(行号)数据库中检索这个表,我用下面的代码:
<预类=郎-SQL prettyprint-覆盖>
SELECT * FROM(
选择ROW_NUMBER()OVER(ORDER BY SNO ASC)为rowIndex位置,SNO,姓名,年龄$ b $从tblExample b)
作为例子,((的PageIndex之间的rowIndex * 10)+1)和((的PageIndex + 1)* 10)
请注意,的PageIndex是可变这需要由用户通过一些整数值。
我的数据库是SQL Server 2008中我想写使用LINQ相同的查询。我该怎么做呢?
解决方案
您可以编写查询作为beow
VAR指数= 1;
变种的PageIndex = 1;
变种的pageSize = 10;
data.Select(X =>新建
{
的rowIndex =指数++,
斯诺= x.Sno,
名称= x.Name,
年龄= x.Age
})排序依据(X => x.Name)
.Skip(pageSize的*(的PageIndex - 1)。)拿(pageSize的).ToList();
My table consists of three columns (sno,name,age). I am retrieving this table from the database with extra column (row number) and I used the following code:
select * from (
select ROW_NUMBER() over (order by SNo asc)as rowindex,SNo,Name,Age
from tblExample)
as example where rowindex between ((pageindex*10)+1) and ((pageindex+1)*10)
Note, pageindex is the variable that takes some integer value which is passed by the user.
My database is Sql Server 2008. I want to write the same query using Linq. How do I do that?
解决方案
You can write query as beow
var index=1;
var pageIndex=1;
var pageSize = 10;
data.Select(x => new
{
RowIndex = index++,
Sno = x.Sno,
Name = x.Name,
Age = x.Age
}).OrderBy(x => x.Name)
.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
这篇关于如何翻译使用ROW_NUMBER()到LINQ查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文