public async Task <HttpResponseMessage> GetAllCatsGroupByGender(Constants.GroupBy groupBy) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); try { response = Request.CreateResponse(HttpStatusCode.OK, await _petService.GetAllCatsAsync(groupBy)); } catch (Exception ex) { response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); } return(response); }
public async Task <IEnumerable <GenderPetsResponseDto> > GetAllCatsAsync(Constants.GroupBy groupBy) { var result = new List <GenderPetsResponseDto>(); var people = await _peopleService.GetAllAsync(); if (people != null && people.Any()) { //LINQ var groupedObjects = people .Where ( pep => pep.pets != null && pep.pets.Any ( pet => String.Equals(pet.type, "Cat", StringComparison.CurrentCultureIgnoreCase) ) ) .GroupBy(pep => pep.gender) .Select(newGroup => new { Gender = newGroup.Key, Pets = newGroup .SelectMany(pep => pep.pets.Where(pet => String.Equals(pet.type, "Cat", StringComparison.CurrentCultureIgnoreCase))) .OrderBy(pet => pet.name) }); //MAP TO DTO result.AddRange ( groupedObjects .Select ( groupedObject => new GenderPetsResponseDto() { Gender = groupedObject.Gender, Pets = groupedObject.Pets } ) ); } return(result); }