При использовании
Код: Выделить всё
int totalRecords = devices.Count();

Для данные с разбивкой на страницы в таблице ниже для таблиц данных

Здесь находится мой код контроллера C# для начальной загрузки и когда пользователь достигает последней страницы:
Код: Выделить всё
public async Task Index(int page = 1)
{
// Check if session exists and retrieve the username
var username = HttpContext.Session.GetString("Username");
if (username == null)
{
// If no session exists, redirect to the Account Index page
return RedirectToAction("Index", "Account");
}
var devices = await _context.GetDeviceMigrationsAsync();
int totalRecords = devices.Count();
int start = 0;
int length = 5000; // Total data to fetch in each request
// Order the devices by LastUpdatedTime in descending order
var orderedDevices = devices.OrderByDescending(d => d.LastUpdatedTime);
// Perform pagination on the server side
var paginatedDevices = orderedDevices.Skip(start).Take(length).Select(d => new DeviceMigration
{
ID = d.ID,
DeviceSerial = d.DeviceSerial ?? "-",
DeviceName = d.DeviceName ?? "-",
UserName = d.UserName ?? "-",
EmployeeID = d.EmployeeID ?? "-",
UserEmail = d.UserEmail ?? "-",
OverallStatus = d.OverallStatus ?? "-",
LastUpdatedTime = d.LastUpdatedTime,
WaveNumber = d.WaveNumber ?? "-"
}).ToList();
// Prepare distinct wave numbers
var waveNumbers = devices
.Where(d => !string.IsNullOrEmpty(d.WaveNumber))
.Select(d => d.WaveNumber)
.Distinct()
.ToList();
var statusCounts = devices
.Where(d => !string.IsNullOrEmpty(d.OverallStatus)) // Filter out null or empty statuses
.GroupBy(d => d.OverallStatus)
.Select(g => new { Status = g.Key, Count = g.Count() })
.ToDictionary(sc => sc.Status, sc => sc.Count);
// Find the minimum ID
int minId = paginatedDevices.Min(d => d.ID);
// Output the minimum ID (optional: for debugging or logging)
Console.WriteLine("Minimum ID: " + minId);
// Pass data to the view
ViewBag.WaveNumbers = waveNumbers;
ViewBag.StatusCounts = statusCounts;
ViewBag.TotalCounts = totalRecords;
return View(paginatedDevices);
}
public async Task GetMoreDeviceData(int page = 1, int pageSize = 5000, int lastDataId = 0)
{
// Get devices ordered by ID to ensure correct pagination based on last ID
var devices = await _context.GetDeviceMigrationsAsync();
// Filter devices where ID is greater than lastDataId
var filteredDevices = devices
.Where(d => d.ID < lastDataId)
.OrderByDescending(d => d.LastUpdatedTime); // Order by ascending ID
// Count the total number of filtered devices
int filteredDevicesCount = filteredDevices.Count();
// Take the next 5000 or fewer records after lastDataId
var paginatedDevices = filteredDevices.Take(pageSize).Select(d => new DeviceMigration
{
ID = d.ID,
DeviceSerial = d.DeviceSerial ?? "-",
DeviceName = d.DeviceName ?? "-",
UserName = d.UserName ?? "-",
EmployeeID = d.EmployeeID ?? "-",
UserEmail = d.UserEmail ?? "-",
OverallStatus = d.OverallStatus ?? "-",
LastUpdatedTime = d.LastUpdatedTime,
WaveNumber = d.WaveNumber ?? "-"
}).ToList();
// Return the data as JSON
return Json(new { data = paginatedDevices });
}
Поскольку при выполнении запроса SQL, как в базе данных, общее количество возвращается как 5181, но когда я фиксирую минимальный идентификатор после начальной загрузки данных и после этого запускаю запрос, он показывает, что осталось 178 строк.
Любая помощь будет принята с благодарностью.
Подробнее здесь: https://stackoverflow.com/questions/790 ... plete-data
Мобильная версия