Refactoring the solution by extracting the conditional expression to a query method