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?
LINQ that stands for Language Integrated Query (pronounced as “link”) is a . NET language extension that supports data retrieval from different data sources like XML document, databases and collections. It was introduced in the . NET 3.5 framework.
More importantly: when it comes to querying databases, LINQ is in most cases a significantly more productive querying language than SQL. Compared to SQL, LINQ is simpler, tidier, and higher-level.
LINQ to SQL is a component of . NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects. Relational data appears as a collection of two-dimensional tables (relations or flat files), where common columns relate tables to each other.
The main difference between LINQ and SQL is that LINQ is a Microsoft . NET framework component, which adds native data querying capabilities to . NET languages, while SQL is a standard language to store and manage data in RDBMS.
You initialized the page number as 0 so -1 is the skip parameter it's complaining about. Initialize the page as 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With