Exemplo n.º 1
0
        private PetNamesByGender GetPetNametListByAnimalTypeAndOwnerGender(List <PetOwner> petOwners, string animalType, string petOwnerGender)
        {
            if (string.IsNullOrEmpty(petOwnerGender))
            {
                throw new ArgumentNullException(@"Error: petOwnerGender must be specified.");
            }

            if (string.IsNullOrEmpty(animalType))
            {
                throw new ArgumentNullException(@"Error: animalType must be specified.");
            }

            var result = new PetNamesByGender();

            result.Gender = petOwnerGender;

            if (petOwners != null && petOwners.Count > 0)
            {
                var correctGender = petOwners.Where(po => po.gender.ToLower().Equals(petOwnerGender.ToLower())).ToList();

                // Note: Null check on pets because pets is nullable property
                var correctGenderAndHasPets = correctGender.Where(x => x.pets != null).SelectMany(x => x.pets).ToList();

                var correctGenderAndHasPetsOfCorrectType =
                    correctGenderAndHasPets.Where(p => p.type.ToLower().Equals(animalType.ToLower())).ToList();

                var petNamesList =
                    correctGenderAndHasPetsOfCorrectType.OrderBy(x => x.name).Select(p => p.name).ToList();

                result.PetNameList = petNamesList;
            }

            return(result);
        }
        /// <summary>
        /// Get pet names by gender
        /// </summary>
        /// <param name="type">string</param>
        /// <returns>List of PetNamesByGender</returns>
        public List <PetNamesByGender> GetPetsByGender(string type)
        {
            var result      = new List <PetNamesByGender>();
            var gendersList = GetGenders();

            foreach (var gender in gendersList)
            {
                var petsByGender = new PetNamesByGender();
                petsByGender.Gender = gender;

                petsByGender.PetNames = _josnDataProvider.GetPeople()
                                        .Where(e => e.Gender.ToLower() == gender.ToLower() && e.Pets != null)
                                        .SelectMany(r => r.Pets)
                                        .Where(e => e.Type.ToLower() == type.ToLower())
                                        .Select(r => r.Name)
                                        .OrderBy(r => r).ToList();

                result.Add(petsByGender);
            }

            return(result);
        }