Biarity / Sieve

⚗️ Clean & extensible Sorting, Filtering, and Pagination for ASP.NET Core

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Total records found

spiotr12 opened this issue · comments

Is there or is it planned to get total number of records found by filter?

Thanks

Why not use LINQ (eg. result.Count())?

From my own experience I'd like the ability to get the total count while filtering and paginating.
When paginating result.Count() will be the number of records on that page, not the total number of records. This alone doesn't give enough information to, say, power a pagination component as we don't know the number of total pages/records.

Though, if I'm wrong I'd be pretty happy as I'm currently doing this as a workaround:

        public async Task<IActionResult> GetQuestions(SieveModel sieveModel)
        {
            var questions = _context.Questions.AsNoTracking();
            questions = _sieveProcessor.Apply(sieveModel, questions);

            var total = _context.Questions.AsNoTracking();
            total = _sieveProcessor.Apply(sieveModel, total, applySorting: false, applyPagination: false);
            Request.HttpContext.Response.Headers.Add("X-Total-Count", total.Count().ToString());

            return Ok(questions);
        }

You're close, here's a better version:

var questions = _context.Questions.AsNoTracking();

questions = _sieveProcessor.Apply(sieveModel, questions, applyPagination: false);

Request.HttpContext.Response.Headers.Add("X-Total-Count", questions.Count().ToString());

questions = _sieveProcessor.Apply(sieveModel, questions, applyFiltering: false, applySorting: false); // Only applies pagination

return Ok(questions);

You filter/sort then apply pagination once you get the count. That's actually how you're intendend to do it, not just a workaround. Tell me if you find a problem with this approach otherwise close the issue :).

Ah, that's much nicer than duplicating the query!
Putting some documentation around that feature to steer people into the right implementation would be helpful!

Hey , reviving this with a question to @Biarity , how was it planned to use Sieve in an API without knowing the total number of pages? There is also not a "hasPrevPage" and "hasNextPage" kind of data.
Will use this solution for now. But just wondered how it's really meant to be used

Yeah I do think this should be core functionality. But the work-around works for me.