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

[Question] If I needed to return total count for pagination, where is the best place to implement? #156

Closed
davidhenley opened this issue Sep 15, 2021 · 8 comments

Comments

@davidhenley
Copy link
Contributor

davidhenley commented Sep 15, 2021

If I am using pagination to skip and take based on page size, but still need to return total items to the client, where should I implement this?

I want to run count after the where filter so I get the actual count of items returned from the search but before my pagination.

In the specification? or run Count() on the entire table first then use the specification?

Do I need a filter specification, and then another pagination specification?

        public GetCustomersForListSpecification(QueryParams query)
        {
            if (!string.IsNullOrWhiteSpace(query.Filter))
                Query.Where(x => x.Name.Contains(query.Filter));

           **********
           NEED TO CALCULATE TOTAL COUNT HERE BEFORE PAGINATION
           **********

            Query
                .Select(x => new CustomerForListResponse
                {
                    Id = x.Id,
                    Name = x.Name
                })
                .Skip((query.Page - 1) * query.PerPage)
                .Take(query.PerPage);
        }

I was using this on the IQueryable:

var source = _context.Customers.Where(x => x.Name.Contains(query.Filter));
var count = await source.CountAsync();
var items = await source.Skip((query.Page - 1) * query.PerPage).Take(query.PerPage).ToListAsync();

Is this a job for an evaluator? I'm trying to find documentation on this

@davidhenley davidhenley changed the title [Question] If I needed to return total count of my query, where is the best place to implement? [Question] If I needed to return total count for pagination, where is the best place to implement? Sep 15, 2021
@davidhenley
Copy link
Contributor Author

Duplicate #134

@davidhenley
Copy link
Contributor Author

davidhenley commented Sep 15, 2021

EDIT: Can we add this to docs? A simple example would be fine for best practices?

Thank you for this amazing package! It will slim my code down a lot.

@davidhenley davidhenley reopened this Sep 15, 2021
@fiseni
Copy link
Collaborator

fiseni commented Sep 15, 2021

Here is the issue #135. I tried to elaborate it, we'll need to add it to the docs.

@davidhenley
Copy link
Contributor Author

davidhenley commented Sep 15, 2021

Thanks @fiseni I’m playing with it now.

I just came upon this package and trying to grok it.

Are the 2 YouTube videos currently the best way to learn more about extending the specs and evaluators?

@fiseni
Copy link
Collaborator

fiseni commented Sep 15, 2021

Not really to be honest. Perhaps we need to create a new video, a planned one :) We recorded the last ones on the fly with no particular agenda.

I know we're lacking good docs. We started working on it, but we're far from finished.
https://ardalis.github.io/Specification/

@davidhenley
Copy link
Contributor Author

OK, thank you! Once I understand everything going on I'll see if I can create some PR's!

@ardalis
Copy link
Owner

ardalis commented Sep 16, 2021

If you want to add/update anything in the docs, they're just markdown files here:
https://github.com/ardalis/Specification/tree/main/docs

Thanks!

@tnlthanzeel
Copy link

hi all. im having a scenario where i start the specification from the aggregate root and include children with skip and take. it works fine. but how can i get the totalrecord count of the children using this library?

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

4 participants