private async Task <int?> GetRandomJokeId(string category, IJokeRepository jokeRepository, IUnitOfWork uow) { var categoryRepository = uow.GetRepository <ICategoryRepository>(); ICollection <int> jokesIds = null; if (!string.IsNullOrEmpty(category)) { if (!await categoryRepository.AnyAsync(c => c.Title.Equals(category))) { throw new ArgumentException($"No jokes for category \"{category}\" found."); } else { jokesIds = await jokeRepository.FindAndMapAsync(j => j.Id, j => j.JokeCategories.Any(jc => jc.Category.Title.Equals(category))); } } else { jokesIds = await jokeRepository.FindAndMapAsync(j => j.Id); } int?randomId = null; if (jokesIds != null && jokesIds.Count() != 0) { randomId = GetRandomElement(jokesIds); } return(randomId); }