-
Notifications
You must be signed in to change notification settings - Fork 246
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
How to count the number of query results #134
Comments
Hi @dmitryst, We implemented a feature where evaluators are aware if they should be processed for Count operation or not. We have this public virtual async Task<int> CountAsync(ISpecification<T> specification, CancellationToken cancellationToken = default)
{
return await ApplySpecification(specification, true).CountAsync(cancellationToken);
}
protected virtual IQueryable<T> ApplySpecification(ISpecification<T> specification, bool evaluateCriteriaOnly = false)
{
return specificationEvaluator.GetQuery(dbContext.Set<T>().AsQueryable(), specification, evaluateCriteriaOnly);
} The issue is that we have only for specifications that don't contain projections, the |
Hi @fiseni , I tried to implement
But I get compile-time error: "CS0266: Cannot implicitly convert type System.Linq.IQueryable{T} to System.Linq.IQueryable{TResult}". I guess that was the issue, you've mentioned. |
Hm, that's because the method which evaluates specs with the selector in public virtual IQueryable<TResult> GetQuery<T, TResult>(IQueryable<T> query, ISpecification<T, TResult> specification) where T : class
{
query = GetQuery(query, (ISpecification<T>)specification);
return query.Select(specification.Selector);
} |
Yeah, I see that there is no flag I would appreciate if you add this feature in the following days. I can wait. |
I just checked the code. Actually, you don't have to do anything. Since var itemsSearchSpec = new ItemsBySearchRequestSpec(request);
var items = await _repository.ListAsync(itemsSearchSpec, cancellationToken);
var count = await _repository.CountAsync(itemsSearchSpec, cancellationToken);
var result = new ItemsSearchResponse
{
Count = count,
PageNumber = request.PageNumber,
Items = items
}; The |
Yes, it works. Thank you for your help and awesome library. |
Sorry to bump an old thread. I'm happy to create a new issue if it's appropriate. It's not clear to me how to get the Count using an In Memory collection. I'd like to keep my API endpoints consistent regardless of if the resource is in memory or remote (supporting ordering, pagination, etc.). Do I have to create a custom Repository implementation which applies the same logic as the provided |
I need to execute query that should contain pagination data.
The
request
model is the following:The
response
model is the following:To return such a response, I use two specifications and then combine the result into
response
model.I would like to know:
Where
clause in one place?The text was updated successfully, but these errors were encountered: