public virtual async Task <List <PetNameByGender> > CatsByGender() { try { // Retrieve data from API and deserialize string jsonResponse = await AppHelper.APIGetData(_appSettings.Value.CatsMediaType, _appSettings.Value.CatsUrl, APIResponseType.Json); List <Owner> ownwers = JsonConvert.DeserializeObject <List <Owner> >(jsonResponse); // Avoid invalid response if (ownwers == null) { return(new List <PetNameByGender>()); } // Filter and retrieve required Data, finally remove duplicates List <PetNameByGender> cats = ownwers.Where(p => p.Pets != null).SelectMany(m => m.Pets.Where(n => (n.Type == PetType.Cat)).Select(o => new PetNameByGender() { OwnerGender = m.Gender.ToString(), PetName = o.Name })).Distinct().ToList(); return(cats); } catch (Exception ex) { // Log errors // Throw exception throw new Exception("Error retrieving data"); } }