Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Skip() and Take() with IQueryable

I have a user control that contains a repeater and it's datasouce is set using an IEnumerable object that contains data returned from query in code. The repeater has paging functionality and displays a custom number of records for each page.

I don't want to load all the data each time the user clicks the next button to see the next page of records in the repeater. How can I make it IQueryable and use Skip() and Take() to display only the records needed for that page?

I have the following code:

//Code that assigns query to repeater data source
DataSet = QueryGoesHere.ToArray(); // returns IEnumerable
repeater.DataSource = DataSet;
repeater.DataBind();

//Code that creates PagedDataSource - how can I update this to make it display only the records that are needed for the currently displayed page?

            objPds = new PagedDataSource();
            objPds.DataSource = DataSource
            objPds.AllowPaging = true;
            objPds.PageSize = 5;
            objPds.CurrentPageIndex = CurrentPage;
            lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objPds.PageCount.ToString();
like image 840
Theomax Avatar asked Sep 17 '11 09:09

Theomax


People also ask

Which is faster IQueryable or IEnumerable?

As IEnumerable performs a lot more work than IQuerytable, it is much slower. In the process of querying data from databases, IQueryable can be seen executing a select query on the server-side with the help of its filters. In comparison to IEnumerable, it does less work and therefore showcases faster performance.

Which is better IEnumerable or IQueryable?

So if you working with only in-memory data collection IEnumerable is a good choice but if you want to query data collection which is connected with database `IQueryable is a better choice as it reduces network traffic and uses the power of SQL language.

Does IQueryable implement IEnumerable?

The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed. The definition of "executing an expression tree" is specific to a query provider.

When should I use IQueryable and IEnumerable using Linq?

In LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by IQueryable, Hence IQueryable has all the features of IEnumerable and except this, it has its own features. Both have its own importance to query data and data manipulation.


1 Answers

if I get you right you want to use your own implementation istead of loading all data and then using the PagedDataSource right?

If so you have to make sure that QueryGoesHere is a Queryable supporting this (Linq2Sql or EF). Then you have to get the count of your date like this

var count = QueryGoesHere.Count();

and get the portion of data you want to display:

var skip = (curPageNumber - 1)*itemsPerPage;
var display = Math.Min(count - skip, itemsPerPage);

and just use

var displayedItems = QueryGoesHere.Skip(skip).Take(display).ToArray();

That should do the trick.

like image 135
Random Dev Avatar answered Sep 22 '22 01:09

Random Dev