【问题标题】:row number with help of linq在 linq 的帮助下的行号
【发布时间】:2013-05-16 17:02:56
【问题描述】:

我的表由 3 列(sno、name、age)组成,现在我正在从数据库中检索带有额外列(行号)的表,我使用了以下代码

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)

注意:这里的 pageindex 是一个变量,它接受用户传递的一些整数值

我的数据库是 sql server 2008,现在我想使用 linq

编写相同的查询

谁能把上面的sql查询改成linq。我无法做到这一点,因为我是 linq 的新手。我遇到了这个问题,请帮助我提前谢谢你

【问题讨论】:

标签: linq linq-to-sql


【解决方案1】:

你可以这样写查询

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

【讨论】:

  • 感谢您的关注。我复制了你的代码。我在数据附近遇到错误。选择“当前上下文中不存在数据
  • data 是您来自 EntityDataModel 的 tblExample 对象,并确保您已导入 System.Linq 命名空间
  • 谢谢你帮了我很多:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多