0

How can I get two value per get request from a List<>, For example, I have a Person List<> with six Person Ordered by ID in that List<> now in every get request using JQuery ajax() I want to get only two item from the list<>, first request will return Person with ID 1 & 2, second request will return Person with ID 3 & 4, and so forth.

I've tried

public IEnumerable<PersonViewModel> GetAllStudents() { IEnumerable<Person> dbPersons = _repo.getPersons().OrderBy(s => s.ID).Take(2); List<PersonViewModel> persons = new List<PersonViewModel>(); foreach(var p in dbPersons) { persons.Add(MapDbPieToPersonViewModel(p)); } return persons; } 

But this only return first two item.

3

1 Answer 1

0

You should use Enumerable.Skip(Int32)

public IEnumerable<PersonViewModel> GetAllStudents(int page) { const int PageSize = 2; IEnumerable<Person> dbPersons = _repo.getPersons().OrderBy(s => s.ID).Skip(page * PageSize).Take(PageSize); List<PersonViewModel> persons = new List<PersonViewModel>(); foreach(var p in dbPersons) { persons.Add(MapDbPieToPersonViewModel(p)); } return persons; } 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, how can I pass the page from my ajax(), I'm new to ajax().
by adding ?page=X to the query string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.