Paging with Linq for Objects

Paging with LINQ for objects

You're looking for the Skip and Take extension methods. Skip moves past the first N elements in the result, returning the remainder; Take returns the first N elements in the result, dropping any remaining elements.

See MSDN for more information on how to use these methods: http://msdn.microsoft.com/en-us/library/bb386988.aspx

Assuming you are already taking into account that the pageNumber should start at 0 (decrease per 1 as suggested in the comments) You could do it like this:

int numberOfObjectsPerPage = 10;
var queryResultPage = queryResult
.Skip(numberOfObjectsPerPage * pageNumber)
.Take(numberOfObjectsPerPage);

Otherwise if pageNumber is 1-based (as suggested by @Alvin)

int numberOfObjectsPerPage = 10;
var queryResultPage = queryResult
.Skip(numberOfObjectsPerPage * (pageNumber - 1))
.Take(numberOfObjectsPerPage);

Paging List of Objects using LINQ

You could use SelectMany:

var page = parents.SelectMany(p => p.Children)
.Skip(PageIndex * PageSize).Take(PageSize);

See a working fiddle here.

Update:

I did some research as this did interest me also. Using the following assumptions:

  • you are using EF
  • you have only one level of this parent->children relationship
  • your entities have all a Position property

you should be able to execute the following against the DB to get the items for a page with the correct order "in one query":

var pageItems = db.Parents.SelectMany(p => p.Children).Include(c => c.Parent)
.OrderBy(c => c.Parent.Position).ThenBy(c => c.Position)
.Skip(PageIndex * PageSize).Take(PageSize);

I did not test this code, as I have no DB here at the moment to test it, so please report back, if you can test it, if the assumtions are correct for your case.

LINQ and pagination

I always use the following code:

public static class PagingExtensions
{
//used by LINQ to SQL
public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, int page, int pageSize)
{
return source.Skip((page - 1) * pageSize).Take(pageSize);
}

//used by LINQ
public static IEnumerable<TSource> Page<TSource>(this IEnumerable<TSource> source, int page, int pageSize)
{
return source.Skip((page - 1) * pageSize).Take(pageSize);
}

}

That is a static class, which you can include in your sources.
After adding this class you can do the following:

MyQuery.Page(pageNumber, pageSize)

Paging using include in Linq to Entities does not work

I would expect the Take to be after the Skip. Edited after tip bij @Hogan.

Have you tried this:

var results = orderBy(collection)
.Skip(parameters.Page * parameters.Rows)
.Take(parameters.Rows)
.ToList();

Linq Paging using Skip and Take returning wrong result

This fixed My error -

  var queryResultPage = eventsResults.Results
.Skip(pageParameters.PageSize * (pageParameters.CurrentPageNumber - 1))
.Take(pageParameters.PageSize).ToList();

Selecting first 10 records, then next 10, paging using Linq

var total = bannersPhrases.Select(p => p.Phrase).Count();
var pageSize = 10; // set your page size, which is number of records per page

var page = 1; // set current page number, must be >= 1 (ideally this value will be passed to this logic/function from outside)

var skip = pageSize * (page-1);

var canPage = skip < total;

if (!canPage) // do what you wish if you can page no further
return;

Phrases = bannersPhrases.Select(p => p.Phrase)
.Skip(skip)
.Take(pageSize)
.ToArray();

List inside List,I want to do pagination of inside list items in c# Linq or Lambda

This answer uses MoreLinq and illustrates the use of "Batch". I understand that the question did not include MoreLinq, but because the question was general, noting "c# Linq or Lambda", I thought that this provided an attractive solution.

result = output.GroupMenuCategories
.SelectMany(MenuItems => MenuItems.SelectMany(x => x))
.Batch(PageSizeSelected).Skip(PageIndex - 1).Take(1).First();


Related Topics



Leave a reply



Submit