From 493cff00fbd550d1e9dd66a5bd1289a712b7d5a3 Mon Sep 17 00:00:00 2001 From: Adam Moussa <166072409+amoussa1229@users.noreply.github.com> Date: Thu, 16 Apr 2026 16:54:18 -0400 Subject: [PATCH] Fix pagination to use database-side Skip/Take instead of loading all rows - Move .ToList() after .Skip().Take() in ContactController, QuotesController, SettingsController - Prevents loading entire tables into memory for paginated queries --- .../Controllers/ContactController.cs | 12 ++++-------- .../Controllers/QuotesController.cs | 4 ++-- .../Controllers/SettingsController.cs | 14 ++++++-------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/Api.SeaHavenIndustries/Controllers/ContactController.cs b/Api.SeaHavenIndustries/Controllers/ContactController.cs index 091b5e9..998ac9e 100644 --- a/Api.SeaHavenIndustries/Controllers/ContactController.cs +++ b/Api.SeaHavenIndustries/Controllers/ContactController.cs @@ -131,12 +131,10 @@ namespace Api.SeaHavenIndustries.Controllers Name = (n.FirstName + " " + n.LastName).Trim(), email = n.Email, phone = n.PhoneNumber - }).OrderByDescending(d => d.Id).ToList().Skip((page - 1) * pageSize) - .Take(pageSize); - // Calculate the number of pages + }).OrderByDescending(d => d.Id).Skip((page - 1) * pageSize) + .Take(pageSize).ToList(); var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize); - // Create a view model containing the paged items and pagination metadata var viewModel = new Pagination_DTO { Data = data, @@ -267,12 +265,10 @@ namespace Api.SeaHavenIndustries.Controllers n.Id, n.Name, n.Title - }).OrderByDescending(d => d.Id).ToList().Skip((page - 1) * pageSize) - .Take(pageSize); - // Calculate the number of pages + }).OrderByDescending(d => d.Id).Skip((page - 1) * pageSize) + .Take(pageSize).ToList(); var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize); - // Create a view model containing the paged items and pagination metadata var viewModel = new Pagination_DTO { Data = data, diff --git a/Api.SeaHavenIndustries/Controllers/QuotesController.cs b/Api.SeaHavenIndustries/Controllers/QuotesController.cs index fd3e09e..311fb33 100644 --- a/Api.SeaHavenIndustries/Controllers/QuotesController.cs +++ b/Api.SeaHavenIndustries/Controllers/QuotesController.cs @@ -174,8 +174,8 @@ namespace Api.SeaHavenIndustries.Controllers s.ContactId, s.Date, lineitems = s.LineItems.Select(q => new { q.Descriptions, q.Lineitem, q.LineitemNumber}), - }).OrderByDescending(d => d.Id).ToList().Skip((page - 1) * pageSize) - .Take(pageSize); + }).OrderByDescending(d => d.Id).Skip((page - 1) * pageSize) + .Take(pageSize).ToList(); // Calculate the number of pages var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize); diff --git a/Api.SeaHavenIndustries/Controllers/SettingsController.cs b/Api.SeaHavenIndustries/Controllers/SettingsController.cs index 78e4189..5a0510a 100644 --- a/Api.SeaHavenIndustries/Controllers/SettingsController.cs +++ b/Api.SeaHavenIndustries/Controllers/SettingsController.cs @@ -104,13 +104,11 @@ namespace Api.SeaHavenIndustries.Controllers { s.Id, s.Name - }).OrderByDescending(d => d.Id).ToList().Skip((page - 1) * pageSize) - .Take(pageSize); + }).OrderByDescending(d => d.Id).Skip((page - 1) * pageSize) + .Take(pageSize).ToList(); - // Calculate the number of pages var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize); - // Create a view model containing the paged items and pagination metadata var viewModel = new Pagination_DTO { Data = data, @@ -129,8 +127,8 @@ namespace Api.SeaHavenIndustries.Controllers try { var totalCount = _db.workOrders.Where(w=> w.istemplate == true).Count(); - var data = _db.workOrders.Where(w => w.istemplate == true).OrderByDescending(d => d.Id).ToList().Skip((page - 1) * pageSize) - .Take(pageSize); + var data = _db.workOrders.Where(w => w.istemplate == true).OrderByDescending(d => d.Id).Skip((page - 1) * pageSize) + .Take(pageSize).ToList(); // Calculate the number of pages var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize); @@ -159,8 +157,8 @@ namespace Api.SeaHavenIndustries.Controllers try { var totalCount = _db.Users.Count(); - var allUsers = _db.Users.Where(w => (w.FirstName ?? "").ToLower().Contains((search ?? "").ToLower())).OrderByDescending(d => d.Id).ToList().Skip((page - 1) * pageSize) - .Take(pageSize); + var allUsers = _db.Users.Where(w => (w.FirstName ?? "").ToLower().Contains((search ?? "").ToLower())).OrderByDescending(d => d.Id).Skip((page - 1) * pageSize) + .Take(pageSize).ToList(); // Calculate the number of pages var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);