---
# System prepended metadata

title: Untitled

---

public class CalculateAverageProductRatingTask : IPipelineTask<EntitiesV2.ProductReview>
{
   public PipelineExecutionResult Execute(EntitiesV2.ProductReview productReview)
   {
      var totalRating = 0;
      int ratingsCount = 0;
      foreach (var review in productReview.Product.ProductReviews)
      {
         if (ReviewShouldCountAgainstOverallRating(review))
         {
            totalRating += review.Rating.Value;
            ratingsCount++;
         }
      }
      productReview.Product.Rating = ratingsCount > 0 ? (double?) totalRating / ratingsCount : null;
      return PipelineExecutionResult.Success;
   }
   private bool ReviewShouldCountAgainstOverallRating(EntitiesV2.ProductReview review)
   {
      if (!review.Rating.HasValue) return false;
      return review.ProductReviewStatus.Name == ProductReviewStatusCode.Approved.ToString();
   }
}