How to use Skip () and Take () functions using IQueryable

I have a user control that contains a relay, and it is installed using an IEnumerable object that contains the data returned from the request in code. The repeater has a swap function and displays a custom number of entries for each page.

I do not want to download all the data every time the user clicks the next button to see the next page of entries in the repeater. How can I do this IQueryable and use Skip () and Take () to display only the records needed for this 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(); 
+5
source share
3 answers

If I get you right, do you want to use your own istead implementation to load all the data, and then using the PagedDataSource right?

If so, you should make sure that QueryGoesHere is Queryable supporting this (Linq2Sql or EF). Then you should get an account of your date like this

 var count = QueryGoesHere.Count(); 

and get the part of the 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.

+5
source

PagedList and its related extensions will be useful to you. Please refer to: http://wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt

+1
source
  public static Dictionary<string, string> SampleDataList(int startIndex, int pageSize) { var sampleTable = new Dictionary<string, string>(); var page = TemporaryData().Skip(startIndex).Take(pageSize); var query = from p in page select new { FirstColumn = p.Key, SecondColumn = p.Value }; foreach (var row in query) sampleTable.Add(row.FirstColumn, row.SecondColumn); return sampleTable; } 

The following link will help you understand how to use pagination with a repeater.

http://cmsnsoftware.blogspot.com/2011/07/how-to-use-custom-pagination.html

+1
source

All Articles