Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq & Paging - Unable to return paged data with an OrderBy

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?

like image 984
GrandMasterFlush Avatar asked Oct 19 '11 14:10

GrandMasterFlush


People also ask

What is LINQ used for?

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.

Is LINQ better than SQL?

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.

What is LINQ to SQL?

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.

What is difference between SQL and LINQ?

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.


1 Answers

You initialized the page number as 0 so -1 is the skip parameter it's complaining about. Initialize the page as 1.

like image 136
Justin Avatar answered Jan 01 '23 16:01

Justin