I am using the following method to get data for my jqgrid's subgrid and its working completely fine.
Note that this method is used to implement server-side sorting and paging.
Now the query I have is as you can see in the line
List<SomeEntity> myList = _service.GetSomeData(id);
here a database call is made and all records are fetched.
So I was just not very sure, So I just wanted to know if this is in with the best practice to implement serverside paging and
public JsonResult GetData(string folderId, string sidx, string sord, int page, int rows) {
int id = int.Parse(folderId);
List < SomeEntity > myList = _service.GetSomeData(id);
const int pageSize = 5;
// total
double totalPages = Math.Ceiling((double) myList.Count() / pageSize);
// sort
if (sord == "asc") {
myList = myList.OrderBy(m = > m.Name).ToList();
}
else {
myList = myList.OrderByDescending(m = > m.Name).ToList();
}
// paging
myList = myList.Skip((page - 1) * pageSize).Take(pageSize).ToList();
var jsonData = new {
total = totalPages, records = domainList.Count, page,
rows = myList
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
_service.GetSomeData(id)
is defined. Do you use Entity Framework or LINQ To SQL? Which type return_service.GetSomeData(id)
? In any way you should not cast the_service.GetSomeData(id)
toList<T>
to process paging and sorting **on the SQL server side and not in your C# program like you do now. – Zambranoonly the very first page request receives the largest amount of data
EXACTLY! The first page will be viewed more often than all of the other pages combined. People usually find what they need on the first page of most paginated results in general, and add into that people going to the page when they didn't actually need to view the results at all (hit the wrong page, didn't know what was there, just looking around, etc.). The first page of any paginated dataset should be optimized as much as possible. – Quadrant