Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newbie LINQ Question: Is Paging in LINQ Queries Possible?

Tags:

c#

linq-to-xml

Is it possible to using "paging" functionality in Linq queries? Let's say I have some XML like this:

<Root>
    <BetaSection>
        <Choices>
            <SetA>
                <Choice id="choice1">Choice One</Choice> 
                <Choice id="choice2">Choice Two</Choice>
                <Choice id="choice3">Choice Three</Choice>
                .
                .
                .
                <Choice id="choice48">Choice Forty-Eight</Choice>
                <Choice id="choice49">Choice Forty-Nine</Choice>
                <Choice id="choice50">Choice Fifty</Choice>
            </SetA>
        </Choices>
    </BetaSection>
</Root>

If I wanted to implement paging functionality, would I be able to provide an offset to a LINQ query such that I could start at the 11th element and end on the 20th element? If so, would the query be any different if the data was a list of objects instead of XML?

like image 575
Bullines Avatar asked Dec 08 '08 20:12

Bullines


People also ask

Which LINQ methods should you use to implement pagination?

We can implement the paging using the Linq Skip and Take method.

What kind of data can be queried with LINQ?

LINQ offers common syntax for querying any type of data source; for example, you can query an XML document in the same way as you query a SQL database, an ADO.NET dataset, an in-memory collection, or any other remote or local data source that you have chosen to connect to and access by using LINQ.

Which of the following are valid reasons to use LINQ?

Readable code: LINQ makes the code more readable so other developers can easily understand and maintain it. Standardized way of querying multiple data sources: The same LINQ syntax can be used to query multiple data sources. Compile time safety of queries: It provides type checking of objects at compile time.

Is LINQ better than stored procedure?

Stored procedures are faster as compared to LINQ query since they have a predictable execution plan and can take the full advantage of SQL features. Hence, when a stored procedure is being executed next time, the database used the cached execution plan to execute that stored procedure.


2 Answers

var q = from X in Choices.Skip((page-1)*pageSize).Take(pageSize)
        select X;

Now, if you need a where clause in it, it gets a bit trickier:

var q = (from X in Choices
         where x.SomeField == SomeValue
         select X).Skip((page-1)*pageSize).Take(pageSize);
like image 171
James Curran Avatar answered Sep 23 '22 05:09

James Curran


Take a look to the Queryable.Skip and Queryable.Take methods.

Also see this useful extension methods for paging,

with that methods you can do this like this:

List<string> names = new List<string>();
names.AddRange(new string[]{"John","Frank","Jeff","George","Bob","Grant", "McLovin"});

foreach (string name in names.Page(2, 2))
{
    Console.WriteLine(name);
}
like image 32
Christian C. Salvadó Avatar answered Sep 19 '22 05:09

Christian C. Salvadó