林克分页 - 无法使用 OrderBy 返回分页数据
我一直在尝试在我编写的 WCF 服务上实现一个简单的分页系统,该服务使用 Linq To SQL 来查询数据库,但似乎从一个问题到另一个问题。
我希望 WCF 服务返回此类型的列表:
[DataContract]
public class TestType
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
}
并且我使用以下代码:
int pageNumber = 0;
int pageSize = 25;
List<TestType> results = (from caseTypes in context.cch
select new TestType()
{
ID = caseTypes.cch_id,
Name = caseTypes.cch_case_ref
}
).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList<TestType>();
但是,当我运行代码时,我收到错误:
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.
因此,如果我更改代码以添加 orderby:
List<TestType> results = (from caseTypes in context.cch
orderby caseTypes.cch_id
select new TestType()
{
ID = caseTypes.cch_id,
Name = caseTypes.cch_case_ref
}
).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList<TestType>();
那么我会受到欢迎通过错误消息:
Count must have a non-negative value.
Parameter name: count
我是否以正确的方式处理此寻呼?
I've been trying to implement a simple paging system on a WCF service I've written that uses Linq To SQL To query a database but seem to be going from one problem to another.
I want the WCF Service to return a list of this type:
[DataContract]
public class TestType
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
}
and I'm using the following code:
int pageNumber = 0;
int pageSize = 25;
List<TestType> results = (from caseTypes in context.cch
select new TestType()
{
ID = caseTypes.cch_id,
Name = caseTypes.cch_case_ref
}
).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList<TestType>();
However, when i run the code I get the error:
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.
So, if I change the code to add an orderby:
List<TestType> results = (from caseTypes in context.cch
orderby caseTypes.cch_id
select new TestType()
{
ID = caseTypes.cch_id,
Name = caseTypes.cch_case_ref
}
).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList<TestType>();
I then get greeted by the error message:
Count must have a non-negative value.
Parameter name: count
Am I even approaching this paging the correct way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将页码初始化为 0,因此 -1 是它所抱怨的跳过参数。将页面初始化为1。
You initialized the page number as 0 so -1 is the skip parameter it's complaining about. Initialize the page as 1.