6.5B Zoo Assignment

6.5B Zoo Assignment
Refactor existing queries to extension methods and use anonymous types
Acceptance Criteria
Acceptance criteria
  • Query extension methods return collections of anonymous objects with the requested information.
1.Refactor existing queries to extension methods and use anonymous types
Problem
Solution
A Add the following extension methods to the ZooExtensions class as specified in the diagram below.
Class Diagram: Zoo Extension Methods. Reference figure for refactor existing queries to extension methods and use anonymous types.
Class Diagram: Zoo Extension Methods
B Write queries in extension methods.
6.5.2 How to Use Anonymous Types
1 In GetYoungGuests, use LINQ syntax to return all guests who are 10 or younger. Return only the following properties
  • Name
  • Age
return
    from g in zoo.Guests
    where g.Age <= 10
    select new { g.Name, g.Age };
2 In GetFemaleDingos, use LINQ syntax to return all dingos who are female. Return only the following properties
  • Name
  • Age
  • Gender
3 In GetHeavyAnimals, use LINQ syntax to return all animals who are heavier than 200 pounds.

Return only the following properties:

  • Type
  • Name
  • Weight
C Write the results to the console using the ForEach extension method.
1 In the QueryHelper in the ConsoleHelper
Subtasks
  • A. Call the appropriate extension methods instead of finding the first young guest, first female dingo, and first heavy animal.
  • B. Change the case-statements to "getyoungguests", "getfemaledingos", and "getheavyanimals."
  • C. Assign the result to a title for the query.

Example: result = "Youngest Guests:\n"

Subtasks
  • D. Use the ToList extention method to turn the collection into a List and then call the ForEach extension method to loop through the list and build up a string that can be output.
D Check Your Work
1 Make sure the Zoo only uses the list of animals and guests from 6.4A.
2 Run the console application application.
3 Get heavy aniamls.

Only animals who are heavier than 200 pounds should be shown, and only the selected properties should be shown. The result should look like the following:

CYW: Get Heavy Animals. Reference figure for refactor existing queries to extension methods and use anonymous types.
CYW: Get Heavy Animals
4 Get female dingos.

Only dingos who are female should be shown, and only the selected properties should be shown. The result should look like the following:

CYW: Get Female Dingoes. Reference figure for refactor existing queries to extension methods and use anonymous types.
CYW: Get Female Dingoes
5 Get young guests.

Only guests who are 10 or younger should be shown, and only the selected properties should be shown. The result should look like the following:

CYW: Get Young Guests. Reference figure for refactor existing queries to extension methods and use anonymous types.
CYW: Get Young Guests
Write LINQ queries with sorting.
Acceptance Criteria
Acceptance criteria
  • The query extension methods return the requested sorted results.
2.Write LINQ queries with sorting.
Problem
Solution
A Add extension methods to the ZooExtensions class as specified in the diagram below.
Class Diagram: Additional Query Methods. Reference figure for write linq queries with sorting.
Class Diagram: Additional Query Methods
B Write queries.
1 In GetGuestsByAge, return all guests but order them by their age. Return only the following properties
  • Name
  • Age
  • Gender
2 In GetFlyingAnimals, return all animals that are flying. Return only the following properties
  • Type
  • Name
3 In GetAdoptedAnimals, return all guests that have an adopted animal. Return only the following properties
  • Guest name
  • Adopted animal name
  • Adopted animal type
Subtasks
  • A. In order for this query to work, Greg and Darla have to adopt animals in the zoo constructor. Have Greg adop Chip the squirrel and have Darla adopt Chum the shark.
Shark chum = new Shark("Chum", 14, 377.3, Gender.Male);
this.AddAnimal(chum);
Squirrel chip = new Squirrel("Chip", 4, 1.0, Gender.Male);
this.AddAnimal(chip);
this.AddAnimal(new Squirrel("Dale", 4, 0.9, Gender.Male));

Guest greg = new Guest("Greg", 35, 100.0m, WalletColor.Crimson, Gender.Male, new Account());
Guest darla = new Guest("Darla", 7, 10.0m, WalletColor.Brown, Gender.Male, new Account());

greg.AdoptedAnimal = chip;
darla.AdoptedAnimal = chum;
this.AddGuest(greg, new Ticket(0m, 0, 0));
this.AddGuest(darla, new Ticket(0m, 0, 0));
C Add three case-statements for each of the three queries in the QueryHelper method on the ConsoleHelper class.

They should be called "getguestsbyage", "getflyinganimals", "getadoptedanimals". Call each extension method in the appropriate case statement, and write the result to the console using the ForEach extension method.

D Check Your Work
1 Run the console application.
2 Get guests by age.

All guests should be shown, but they should be sorted by age in ascending order. The resulting query should look like the following:

Image: GetGuestsByAge Result. Reference figure for write linq queries with sorting.
Image: GetGuestsByAge Result
3 Get flying animals.

Only animals who are flying (e.g. eagles) should be shown. The resulting query should look like the following:

Image: GetFlyingAnimals Result. Reference figure for write linq queries with sorting.
Image: GetFlyingAnimals Result
4 Get adopted animals.

Greg and Darla should appear along with the animal they adopted. Greg should be the adopter and Chip should be the animal. Darla should be the adopter and Chum should be the animal. The resulting query should look like the following:

Image: GetAdoptedAnimals Result. Reference figure for write linq queries with sorting.
Image: GetAdoptedAnimals Result
Write advanced LINQ queries
Acceptance Criteria
Acceptance criteria
  • Advanced queries group, sum, average, and order the specified animal and wallet information.
3.Write advanced LINQ queries
Problem
Solution
A Add extension methods to the ZooExtensions class as specified in the diagram below.
Class Diagram: Advanced Queries. Reference figure for write advanced linq queries.
Class Diagram: Advanced Queries
B Write queries.
1 Write the GetTotalBalanceByWalletColor query that returns the values below
  • Group key (wallet color)
  • Total balance for wallet color
Subtasks
  • A. Write the return statement and the from g in zoo.Guests statement.
  • B. Write the group statement:
group g by g.Wallet.Color.ToString() into grby g.Wallet.Color.ToString() into gr
Subtasks
  • C. Order by the grouping's Key.
orderby gr..Key
Subtasks
  • D. Select the Key (the wallet color) and the TotalMoneyBalance by using the Sum extension method on the grouping and passing in the Guest object with their Wallet's MoneyBalance.
select new { gr.Key, TotalMoneyBalance = gr.Sum(g => g.Wallet.MoneyBalance) };
2 In GetAverageWeightByAnimalType, return all animals grouped and ordered by their types.

Return only the following properties:

  • Group key (animal type)
  • Average weight for type
C Create the case-statements "totalbalancebycolor" and "averageweightbyanimaltype".

Call each extension method in the appropriate case-statments, and write the results to the console.

D Check Your Work
1 Run the console application.
2 Get the total balance by wallet color.

Only the wallet colors that existing guests have should be shown. The wallet money balance for all guests with that color wallet should be totaled. The resulting query should look like the following:

Image: Total Balance by Wallet Color. Reference figure for write advanced linq queries.
Image: Total Balance by Wallet Color
3 Get the average weight by animal type.

Only the types of existing animals should be shown (one for each type, not one for each animal). The weight for all animals of that type should be averaged. The resulting query should look like the following:

Image: Average Weight by Animal Type. Reference figure for write advanced linq queries.
Image: Average Weight by Animal Type
Demonstrate your work
4.Record a video
A Show the places LINQ queries were used
1 ZooExtension class, methods
Subtasks
  • A. GetAdoptedAnimals
  • B. GetAverageWeightByAnimalType
  • C. GetFemaleDingos
  • D. GetFlyingAnimals
  • E. GetGuestsByAge
  • F. GetHeavyAnimals
  • G. GetTotalBalanceByWalletColor
  • H. GetYoungGuests
Prepare and submit your work
5.Submission Steps
A Ensure that your application has no compiler errors or warnings
B Ensure that your code is StyleCop compliant
C Remove all bin and obj folders from your solution
D Zip your solution
E Submit your zipped solution to the Feedback System
F Submit your Feedback System results link to Canvas
G Submit your video recording to your f-channel
Source credit

© 2026 Northcentral Technical College