Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Total records found #34

Closed
spiotr12 opened this issue Jul 6, 2018 · 6 comments
Closed

Total records found #34

spiotr12 opened this issue Jul 6, 2018 · 6 comments

Comments

@spiotr12
Copy link

spiotr12 commented Jul 6, 2018

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

Thanks

@Biarity
Copy link
Owner

Biarity commented Jul 8, 2018

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

@zolrath
Copy link
Collaborator

zolrath commented Jul 10, 2018

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);
        }

@Biarity
Copy link
Owner

Biarity commented Jul 10, 2018

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 :).

@zolrath
Copy link
Collaborator

zolrath commented Jul 10, 2018

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!

@vengaglobal
Copy link

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

@stefankip
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants