'Entity Framework Core Count related entities in a single query

I am working on a project where I have following Entities

public class Product
{
     // all the other properties have been removed

     ......

     public IReadOnlyCollection<ProductReview> Reviews => _reviews.AsReadOnly();
}

In my service layer, I query list of products and map it to a paged list productdto (using automapper ProjectTo extension). My query looks something like this

return await _context.Products
   .ProjectTo<ProductDto>(_mapper.ConfigurationProvider)
   .ToPagedList(request.page, request.pageSize);

Now one of the requirement that came up is I need to show Reviews Count with each Product. I am thinking whats the best way to count the Reviews in this situation without re-writing the above query, mapping, paging logic?

Would it be good idea to put "ReviewCount" property in the Product entity and keep updating it anytime the List of Reviews is updated? That way I won't have to make any Join to count the reviews.

I can write a query to separately count the Review but I am not sure how am I going to use the current ProjectTo and Paging extension in that case.

Any suggestion would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source