Drupal 8 Looking for an elegant alternative of rowCount in a query In Drupal 8 we can not use the rowCount property anymore in case of a select statement. Is there an elegant alternative?

I can use a foreach, but when converting from Drupal 7 I would like to replace the result->rowCount() more elegantly
According to the Drupal API, "in D8 rowCount() is the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed or throws \Drupal\Core\Database\RowCountException if the last executed statement was SELECT."

If you need to select count of rows you can use $query->addExpression('COUNT(*)');

   $query = \Drupal::database()-&gt;select('table', 't');
   $query-&gt;addExpression('COUNT(*)');
   $count = $query-&gt;execute()-&gt;fetchField();