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();
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.
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.
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.
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.
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.
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