1

I have the below list made from my database which will bring back x number results. I would like to split the result into list of 15 which will have List<List<SubjectModel>> split list model.
I would like to know who I can do this, as there may not complete spilt as 15 as the final will have less. Also on MVC if I was to create a paging effect how can this be done by calling a diffent index of data to be loaded or will I have to go to the server each time.
I presume that I will have to have a parial page within a page.

List<SubjectModel> model = new List<SubjectModel>();
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
manu1
  • 31
  • 4
  • possible duplicate of [Split List into Sublists with LINQ](http://stackoverflow.com/questions/419019/split-list-into-sublists-with-linq) – Mark Jansen Aug 03 '15 at 09:03
  • I do not recommend mark this duplicate as the real answer here will be: Do not fetch all, then split, instead do a real paging – g.pickardou Aug 03 '15 at 09:06

1 Answers1

0

Generally you can use the .Skip() and .Take() LINQ methods in the following way:

.Skip(pageSize * pageNumber - 1).Take(pageSize) 

I recommend do not fetch all, then split, instead do a real paging using the methods above. When using those methods against a real O/R mapper like Entity Framework, it will translate to your RDBMS's SQL.

You can use action method parameters in your controller to know what page was requested. Your pageSize could be a constant or a configuration value.

You can start learn the whole concept by googling: [implement paging in asp.net mvc]

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • so when i use this method i will getting the current page adding one and passing it back to the server geting the next page. how would i set this up in the conroller. – manu1 Aug 03 '15 at 09:55